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 } })
},
},
},
3 Replies
nikatune
nikatune5mo 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 DrummOP5mo ago
sorry yes after wrapping a try/catch i realized the issue is with the set active, will try that, thanks
ubinatus
ubinatus3mo ago
Hi @Sean Drumm When you do the auth.api.createOrganization inside the after user create hook, don't you have 401 (unauthorized) issues? Currently trying to do the same but without any success

Did you find this page helpful?