fix: Set organization after creation

This commit is contained in:
2025-08-11 06:30:00 +02:00
parent 825ed64b0e
commit 851d0fef46
2 changed files with 30 additions and 8 deletions

View File

@@ -36,7 +36,9 @@
import * as z from 'zod' import * as z from 'zod'
import { useBetterAuth } from '~/composables/useBetterAuth' import { useBetterAuth } from '~/composables/useBetterAuth'
const emit = defineEmits(['formSubmitted']) const emit = defineEmits<{
(e: 'organizationCreated', id: string | undefined): void
}>()
const { createOrganization } = useBetterAuth() const { createOrganization } = useBetterAuth()
const open = ref(false) const open = ref(false)
@@ -92,8 +94,8 @@ async function onSubmit() {
if (!state.name || !state.slug) return if (!state.name || !state.slug) return
await createOrganization(state.name, state.slug, state.logo) const organization = await createOrganization(state.name, state.slug, state.logo)
emit('formSubmitted', { name: state.name, slug: state.slug }) emit('organizationCreated', organization.data?.id)
loading.value = false loading.value = false
open.value = false open.value = false
} }

View File

@@ -29,7 +29,7 @@
placeholder="Select organization" placeholder="Select organization"
class="w-64" class="w-64"
/> />
<CreateOrganizationModal /> <CreateOrganizationModal @organization-created="setOrganization" />
</div> </div>
</UPageCard> </UPageCard>
@@ -147,18 +147,38 @@ const { copy, copied } = useClipboard()
const toast = useToast() const toast = useToast()
const { organization } = useAuth() const { organization } = useAuth()
const organizations = (await organization.list()).data || []
const { deleteOrganization: betterAuthDeleteOrganization, activeOrganization, selectedOrgId } = useBetterAuth() const { deleteOrganization: betterAuthDeleteOrganization, activeOrganization, selectedOrgId } = useBetterAuth()
const availableOrganizations = computed(() => organizations.map((org) => ({ label: org.name, value: org.id })))
const organizations = ref<
Array<{ id: string; name: string; slug: string; createdAt: Date; logo?: string | null; metadata?: unknown }>
>([])
const availableOrganizations = computed(() => organizations.value.map((org) => ({ label: org.name, value: org.id })))
const { user } = await useAuth()
const isRevoking = ref<string[]>([])
watch(selectedOrgId, async (newId) => { watch(selectedOrgId, async (newId) => {
const { data } = await organization.setActive({ organizationId: newId || null }) const { data } = await organization.setActive({ organizationId: newId || null })
activeOrganization.value = data activeOrganization.value = data
}) })
const { user } = await useAuth() await loadOrganizations()
const isRevoking = ref<string[]>([]) async function loadOrganizations() {
try {
const response = await organization.list()
organizations.value = response.data || []
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Failed to load organizations'
toast.add({ title: 'Error', description: errorMessage, color: 'error' })
}
}
async function setOrganization(id: string | undefined) {
await loadOrganizations()
selectedOrgId.value = id
}
function isAdminOrOwner(member: { role: string }) { function isAdminOrOwner(member: { role: string }) {
return member.role === 'owner' || member.role === 'admin' return member.role === 'owner' || member.role === 'admin'