How to get client to immediately update activeOrganizationId after creating an organization?

Hello! I have some code that redirects users to setup an organization after they first sign up
const { data: session, isPending } = authClient.useSession();
if (!isPending && !session?.session.activeOrganizationId) {
redirect("/setup-organization");
}
const { data: session, isPending } = authClient.useSession();
if (!isPending && !session?.session.activeOrganizationId) {
redirect("/setup-organization");
}
When they submit the organization form I call
await authClient.organization.create({
name: data.name.trim(),
slug: generateSlug(data.name.trim()),
});
await authClient.organization.create({
name: data.name.trim(),
slug: generateSlug(data.name.trim()),
});
I expected this to automatically update the session cookie w/ the activeOrganizationId although I found that not to be the case. However, even when I try to explicitly refresh the session before redirecting to the dashboard page it doesn't work
await authClient.getSession({
query: {
disableCookieCache: true,
},
});
await authClient.getSession({
query: {
disableCookieCache: true,
},
});
Only refreshing the page entirely gets activeOrganizationId in the session. Is there any easy way to achieve what I want? What am I missing? Thanks!
3 Replies
David
DavidOP2mo ago
So really there are 3 things I’m confused about 1. Why doesn’t createOrganization update the session cookie? 2. It seems like isPending starts off as false and then the queries for things like useSession and useActiveOrganization are kicked off synchronously. If that’s the case then I’m not sure how you’re supposed to use isPending for rendering components. 3. Why doesn’t explicitly calling get session with the cache disabled work?
Lucas Andrade
Lucas Andrade2mo ago
this works for me, not sure if helpful...
async function getActiveOrganization(userId: string) {
const memberRecord = await db.select({
organizationId: member.organizationId,
})
.from(member)
.where(eq(member.userId, userId))
.limit(1);

return memberRecord.length > 0 ? memberRecord[0].organizationId : null;
}
async function getActiveOrganization(userId: string) {
const memberRecord = await db.select({
organizationId: member.organizationId,
})
.from(member)
.where(eq(member.userId, userId))
.limit(1);

return memberRecord.length > 0 ? memberRecord[0].organizationId : null;
}
databaseHooks: {
session: {
create: {
before: async (session) => {
const organizationId = await getActiveOrganization(session.userId);
return {
data: {
...session,
activeOrganizationId: organizationId,
}
}
}
}
}
}
databaseHooks: {
session: {
create: {
before: async (session) => {
const organizationId = await getActiveOrganization(session.userId);
return {
data: {
...session,
activeOrganizationId: organizationId,
}
}
}
}
}
}
David
DavidOP2mo ago
I solved by calling refetch, I think it is related to https://github.com/better-auth/better-auth/issues/1006
GitHub
useSession() not always triggering a state change · Issue #1006 ...
Is this suited for github? Yes, this is suited for github To Reproduce Sign in with email and password Use useSession() in the following component: export function AuthLayout({ children }: AppLayou...

Did you find this page helpful?