Is there any way to create organization after user create hook?

Im getting UNAUTHORIZED error because im trying to create an organization for user with using user : { create : { after { } }} hook. Is there any way to avoid it?
Solution:
since you have the user id, remove headers and pass userId in the body instead
Jump to solution
8 Replies
rhitune
rhituneOP4w ago
@Ping @bekacru
databaseHooks: {
user: {
create: {
after: async (user) => {
try {
// Generate a slug from user's name or email
const userName = user.name || user.email?.split('@')[0] || 'Default';
const slug = `${userName.toLowerCase().replace(/[^a-z0-9]/g, '-')}-${Date.now().toString().slice(-6)}`;

// Create a default organization for the user
const organization = await auth.api.createOrganization({
headers: await headers(),
body: {
name: `${userName} Organization`,
slug: slug,
logo: 'https://ticketfa.st/logo.png',
metadata: {
createdAt: new Date().toISOString(),
createdBy: user.id,
isDefault: true
}
}
});
databaseHooks: {
user: {
create: {
after: async (user) => {
try {
// Generate a slug from user's name or email
const userName = user.name || user.email?.split('@')[0] || 'Default';
const slug = `${userName.toLowerCase().replace(/[^a-z0-9]/g, '-')}-${Date.now().toString().slice(-6)}`;

// Create a default organization for the user
const organization = await auth.api.createOrganization({
headers: await headers(),
body: {
name: `${userName} Organization`,
slug: slug,
logo: 'https://ticketfa.st/logo.png',
metadata: {
createdAt: new Date().toISOString(),
createdBy: user.id,
isDefault: true
}
}
});
Solution
bekacru
bekacru4w ago
since you have the user id, remove headers and pass userId in the body instead
bekacru
bekacru4w ago
body:{
name: //
//...
userId: user.id
}
body:{
name: //
//...
userId: user.id
}
rhitune
rhituneOP4w ago
oh okeyy thanks
ayersss
ayersss2w ago
hi, I have same question. How can I create an org after new user signs up? In you code snippet how do you get access to auth instance?
Nathan
Nathan2w ago
you can still use the current auth instance, and use the userId: user.id directly like @bekacru , unnecessary to use headers()
rhitune
rhituneOP2w ago
I dont have create organization code but i have set active organization to session for every sign in
databaseHooks: {
session: {
create: {
before: async (session) => {
try {
const user = await getUserInfo(session.userId);
if (user?.isCompletedOnboarding) {
const organization = await getActiveOrganization(session.userId);
return {
data: {
...session,
activeOrganizationId: organization?.id,
},
};
} else {
return {
data: session,
};
}
} catch (error) {
return {
data: session,
};
}
},
},
},
databaseHooks: {
session: {
create: {
before: async (session) => {
try {
const user = await getUserInfo(session.userId);
if (user?.isCompletedOnboarding) {
const organization = await getActiveOrganization(session.userId);
return {
data: {
...session,
activeOrganizationId: organization?.id,
},
};
} else {
return {
data: session,
};
}
} catch (error) {
return {
data: session,
};
}
},
},
},
Its easy to make
ayersss
ayersss2w ago
Thanks for the snippet and answer! I made it working now!

Did you find this page helpful?