auto-creating organization on user signup?

Has anyone managed to do this? Want to ensure users always have an org, i.e. a personal tenant even on signup. I did this through the UI but its pretty clunky. I tried this already:
databaseHooks: {
user: {
create: {
after: async (user) => {
const org = await auth.api.createOrganization({
body: {
name: `${user.name}'s Organization`,
slug: `${Math.random().toString(36).substring(2, 15)}-${user.name}s-organization`,
userId: user.id,
},
})

await auth.api.setActiveOrganization({ body: { organizationId: org?.id } })
},
},
},
databaseHooks: {
user: {
create: {
after: async (user) => {
const org = await auth.api.createOrganization({
body: {
name: `${user.name}'s Organization`,
slug: `${Math.random().toString(36).substring(2, 15)}-${user.name}s-organization`,
userId: user.id,
},
})

await auth.api.setActiveOrganization({ body: { organizationId: org?.id } })
},
},
},
2 Replies
rhitune
rhitune2w ago
Like is there a feature for setting default organization for every login?
@bekacru I want to know use session create before hook like this for set organization after every sign in
session: {
create: {
before: async (session) => {
const organization = await getActiveOrganization(session.userId);
return {
data: {
...session,
activeOrganizationId: organization?.id,
},
};
},
},
},
session: {
create: {
before: async (session) => {
const organization = await getActiveOrganization(session.userId);
return {
data: {
...session,
activeOrganizationId: organization?.id,
},
};
},
},
},
Sean Drumm
Sean DrummOP2w ago
sorry yes after wrapping a try/catch i realized the issue is with the set active, will try that, thanks

Did you find this page helpful?