BA
Better Auth•3mo ago
Amari

activeOrganizationId stays null after setActiveOrganization

Creating org + setting as active works, but session.activeOrganizationId remains null on next request. Create org:
const orgData = await auth.api.createOrganization({
body: {
name: "test",
slug: "test-test",
keepCurrentActiveOrganization: false,
},
headers: await headers(),
});

await auth.api.setActiveOrganization({
headers: await headers(),
body: { organizationId: orgData.id },
});
const orgData = await auth.api.createOrganization({
body: {
name: "test",
slug: "test-test",
keepCurrentActiveOrganization: false,
},
headers: await headers(),
});

await auth.api.setActiveOrganization({
headers: await headers(),
body: { organizationId: orgData.id },
});
const session = await auth.api.getSession({
headers: await headers(),
});
console.log(session?.activeOrganizationId); // null
const session = await auth.api.getSession({
headers: await headers(),
});
console.log(session?.activeOrganizationId); // null
Solution:
Do you use cookie cache?
Jump to solution
20 Replies
Ping
Ping•3mo ago
If you check the DB right after setActiveOrg was called, does the activeOrganizationId say it's true for that session?
Amari
AmariOP•3mo ago
I still get null :( The db has the right activeorgid when I query it directly, but getsession() still gives me null.
Solution
Vlad
Vlad•3mo ago
Do you use cookie cache?
Amari
AmariOP•3mo ago
yea ohh that's whyy
Vlad
Vlad•3mo ago
😄
Amari
AmariOP•3mo ago
but shouldn't the session be updated automatically after creating the org?
Vlad
Vlad•2mo ago
Client doesn't know when something is updated on the server Just refresh the session and that's all
Amari
AmariOP•2mo ago
Even after refreshing the session on the client side with disablecookiecache: true, the activeorgid still returns null :( @Ping
Vlad
Vlad•2mo ago
Show your code
Amari
AmariOP•2mo ago
useEffect(() => {
if (actionState?.success && actionState?.organizationSlug) {
const handleSuccess = async () => {
try {
await authClient.getSession({ query: { disableCookieCache: true } });

router.push(`/dashboard/${actionState.organizationSlug}`);
} catch (error) {
console.error("error:", error);
router.push(`/dashboard/${actionState.organizationSlug}`);
}
};

handleSuccess();
}
}, [actionState?.success, actionState?.organizationSlug, router]);
useEffect(() => {
if (actionState?.success && actionState?.organizationSlug) {
const handleSuccess = async () => {
try {
await authClient.getSession({ query: { disableCookieCache: true } });

router.push(`/dashboard/${actionState.organizationSlug}`);
} catch (error) {
console.error("error:", error);
router.push(`/dashboard/${actionState.organizationSlug}`);
}
};

handleSuccess();
}
}, [actionState?.success, actionState?.organizationSlug, router]);
try {
const orgData = await auth.api.createOrganization({
body: {
name: name,
slug: slug,
logo: logo,
metadata: {
createdDuringOnboarding: true,
},
keepCurrentActiveOrganization: false,
},
headers: useHeaders,
});

if (orgData) {
await auth.api.updateUser({
headers: useHeaders,
body: {
onboarding: true,
},
});

return {
success: true,
organizationSlug: orgData.slug,
organizationId: orgData.id,
};
try {
const orgData = await auth.api.createOrganization({
body: {
name: name,
slug: slug,
logo: logo,
metadata: {
createdDuringOnboarding: true,
},
keepCurrentActiveOrganization: false,
},
headers: useHeaders,
});

if (orgData) {
await auth.api.updateUser({
headers: useHeaders,
body: {
onboarding: true,
},
});

return {
success: true,
organizationSlug: orgData.slug,
organizationId: orgData.id,
};
^^
Ping
Ping•2mo ago
Are you sure you're fetching the session after the active org was set?
Amari
AmariOP•2mo ago
yup
Vlad
Vlad•2mo ago
Is cache cookie changed when you refresh the session?
Amari
AmariOP•2mo ago
lemme check
Amari
AmariOP•2mo ago
Amari
AmariOP•2mo ago
__Secure-domain.session_data cookie did update In the cookie only onboarding got set to true activeorgid stayed the same
Vlad
Vlad•2mo ago
Onboarding is updated, but active org is not?
Amari
AmariOP•2mo ago
yeaa
Vlad
Vlad•2mo ago
Really weird. I thought the problem was cookie cache as it's kinda buggy, but well What if you set active organization manually after creation
Amari
AmariOP•2mo ago
lemme try that

Did you find this page helpful?