BA
Better Auth•2w 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•2w ago
If you check the DB right after setActiveOrg was called, does the activeOrganizationId say it's true for that session?
Amari
AmariOP•2w 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•2w ago
Do you use cookie cache?
Amari
AmariOP•2w ago
yea ohh that's whyy
Vlad
Vlad•2w ago
😄
Amari
AmariOP•2w ago
but shouldn't the session be updated automatically after creating the org?
Vlad
Vlad•2w ago
Client doesn't know when something is updated on the server Just refresh the session and that's all
Amari
AmariOP•7d ago
Even after refreshing the session on the client side with disablecookiecache: true, the activeorgid still returns null :( @Ping
Vlad
Vlad•7d ago
Show your code
Amari
AmariOP•7d 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•7d ago
Are you sure you're fetching the session after the active org was set?
Amari
AmariOP•7d ago
yup
Vlad
Vlad•7d ago
Is cache cookie changed when you refresh the session?
Amari
AmariOP•7d ago
lemme check
Amari
AmariOP•7d ago
Amari
AmariOP•7d ago
__Secure-domain.session_data cookie did update In the cookie only onboarding got set to true activeorgid stayed the same
Vlad
Vlad•7d ago
Onboarding is updated, but active org is not?
Amari
AmariOP•7d ago
yeaa
Vlad
Vlad•7d 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•7d ago
lemme try that

Did you find this page helpful?