121 lines
3.4 KiB
TypeScript
121 lines
3.4 KiB
TypeScript
const activeOrganization = ref<ActiveOrganization | null>(null)
|
|
const selectedOrgId = ref<string | undefined>(undefined)
|
|
|
|
export function useBetterAuth() {
|
|
const toast = useToast()
|
|
|
|
async function createOrganization(name: string, slug: string, logo?: string) {
|
|
await organization.create(
|
|
{ name, slug, logo },
|
|
{
|
|
onSuccess: () => {
|
|
toast.add({ title: 'Organisation erfolgreich erstellt', color: 'success' })
|
|
return Promise.resolve()
|
|
},
|
|
onError: (ctx) => {
|
|
toast.add({
|
|
title: 'Fehler bei der Erstellung der Organisation',
|
|
description: ctx.error.message,
|
|
color: 'error'
|
|
})
|
|
return Promise.reject()
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
async function deleteOrganization() {
|
|
await authClient.organization.delete(
|
|
{ organizationId: activeOrganization.value?.id ?? '' },
|
|
{
|
|
onSuccess: () => {
|
|
toast.add({ title: 'Organization deleted', color: 'success' })
|
|
activeOrganization.value = null
|
|
selectedOrgId.value = undefined
|
|
},
|
|
onError: (ctx) => {
|
|
toast.add({ title: 'Error deleting organization', description: ctx.error.message, color: 'error' })
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
async function getInvitation(invitationId: string): Promise<CustomInvitation> {
|
|
return authClient.organization.getInvitation({
|
|
query: { id: invitationId },
|
|
fetchOptions: {
|
|
throw: true,
|
|
onSuccess: (ctx) => {
|
|
return ctx.data
|
|
},
|
|
onError: (error) => {
|
|
toast.add({ title: 'Fehler beim Einladen', description: error.error.message, color: 'error' })
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
async function inviteMember(email: string, role: 'member' | 'admin') {
|
|
await authClient.organization.inviteMember({
|
|
email,
|
|
role,
|
|
fetchOptions: {
|
|
throw: true,
|
|
onSuccess: (ctx) => {
|
|
if (activeOrganization.value) {
|
|
activeOrganization.value = {
|
|
...activeOrganization.value,
|
|
invitations: [...(activeOrganization.value?.invitations || []), ctx.data]
|
|
}
|
|
}
|
|
},
|
|
onError: (error) => {
|
|
toast.add({ title: 'Fehler beim Einladen', description: error.error.message, color: 'error' })
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
async function acceptInvitation(invitationId: string) {
|
|
await authClient.organization.acceptInvitation({
|
|
invitationId,
|
|
fetchOptions: {
|
|
throw: true,
|
|
onSuccess: async () => {
|
|
toast.add({ title: 'Invitation accepted', color: 'success' })
|
|
await navigateTo('/')
|
|
},
|
|
onError: (ctx) => {
|
|
toast.add({ title: 'Error when accepting invitation', description: ctx.error.message, color: 'error' })
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
async function rejectInvitation(invitationId: string) {
|
|
await authClient.organization.rejectInvitation({
|
|
invitationId,
|
|
fetchOptions: {
|
|
throw: true,
|
|
onSuccess: () => {
|
|
toast.add({ title: 'Invitation rejected', color: 'success' })
|
|
},
|
|
onError: (ctx) => {
|
|
toast.add({ title: 'Error when rejecting invitation', description: ctx.error.message, color: 'error' })
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
return {
|
|
activeOrganization,
|
|
selectedOrgId,
|
|
createOrganization,
|
|
deleteOrganization,
|
|
getInvitation,
|
|
inviteMember,
|
|
acceptInvitation,
|
|
rejectInvitation
|
|
}
|
|
}
|