activeOrganizationId is null

Hi, i've been running into an issue that i've been trying to debug for a couple days now. With the org plugin, activeOrganizationId on the session is (usually) null, causing getActiveMember to break too. Adding the below to my auth config sometimes fixes it, but sometimes it inexplicably breaks (for some orgs specifically).
session: {
create: {
before: async (session) => {
const member = await db.query.members.findFirst({
where: (m, { eq }) => eq(m.userId, session.userId),
});
if (!member) {
return undefined;
}

return {
data: {
...session,
activeOrganizationId: member?.organizationId,
},
};
},
},
},
session: {
create: {
before: async (session) => {
const member = await db.query.members.findFirst({
where: (m, { eq }) => eq(m.userId, session.userId),
});
if (!member) {
return undefined;
}

return {
data: {
...session,
activeOrganizationId: member?.organizationId,
},
};
},
},
},
Anyone have an idea on how to fix this? My auth config is here: https://gist.github.com/Badbird5907/5628d5f881bc57f9d70462bfe232307a The users were created with a script directly inserting the users/orgs into their respective tables in the db, and the users use the google provider to log in. Thanks
2 Replies
Badbird5907
Badbird5907OP2mo ago
I've also noticed that when I try to call setActiveOrganization, It fails with the following error:

⨯ [Error [APIError]: ] {
status: 'UNAUTHORIZED',
body: undefined,
headers: {},
statusCode: 401,
digest: '223797055'
}

⨯ [Error [APIError]: ] {
status: 'UNAUTHORIZED',
body: undefined,
headers: {},
statusCode: 401,
digest: '223797055'
}
I have also tried adding activeOrganizationId as an additional field on session:
session: {
additionalFields: {
activeOrganizationId: { type: "string", required: false, input: false },
},
},
session: {
additionalFields: {
activeOrganizationId: { type: "string", required: false, input: false },
},
},
and the customSession plugin:
customSession(async ({ user, session }) => {
const member = await db.query.members.findFirst({
where: (m, { eq }) => eq(m.userId, user.id),
});
return {
user: user,
session: {
...session,
activeOrganizationId: member?.organizationId,
},
};
}),
customSession(async ({ user, session }) => {
const member = await db.query.members.findFirst({
where: (m, { eq }) => eq(m.userId, user.id),
});
return {
user: user,
session: {
...session,
activeOrganizationId: member?.organizationId,
},
};
}),
This actually adds the activeOrganizationId on the client, but not on the server I believe (when activeOrganizationId is properly set via the db), the first time I run getSession and subsequently getActiveMember, everything works properly. However the second time it is run (for example on a page reload), it getSession returns a session with activeOrganizationId as null, and getActiveMember fails
Badbird5907
Badbird5907OP2mo ago
GitHub
fix: support for organizations with large member counts by Badbird5...
Hi, My app has an organization which has approximately 6,000 members, we've encountered an issue where the activeOrganizationId field is set to null after calling getFullOrganization. More ...

Did you find this page helpful?