Skip to content

Commit

Permalink
fix(chat): reduce session token size (#2027)
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaBondar authored Sep 3, 2024
1 parent dbdfe1d commit 4993760
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion apps/chat/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ declare module 'next-auth' {
declare module 'next-auth/jwt' {
interface JWT extends DefaultJWT {
user?: Partial<{
dial_roles: string[];
isAdmin: boolean;
}>;
access_token?: string;
}
Expand Down
19 changes: 9 additions & 10 deletions apps/chat/src/utils/auth/auth-callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ const safeDecodeJwt = (accessToken: string) => {
const getUser = (accessToken?: string) => {
const rolesFieldName = process.env.DIAL_ROLES_FIELD ?? 'dial_roles';
const decodedPayload = accessToken ? safeDecodeJwt(accessToken) : {};
const adminRoleNames = (process.env.ADMIN_ROLE_NAMES || 'admin').split(',');
const dialRoles = get(decodedPayload, rolesFieldName, []) as string[];
const roles = Array.isArray(dialRoles) ? dialRoles : [dialRoles];
const isAdmin =
roles.length > 0 && adminRoleNames.some((role) => roles.includes(role));

return {
dial_roles: get(decodedPayload, rolesFieldName, []) as string[],
isAdmin,
};
};

Expand Down Expand Up @@ -202,16 +207,10 @@ export const callbacks: Partial<
options.token.error;
}

const dialRoles = options?.token?.user?.dial_roles;
const isAdmin = options?.token?.user?.isAdmin ?? false;

if (options.session.user && dialRoles) {
const roles = Array.isArray(dialRoles) ? dialRoles : [dialRoles];
const adminRoleNames = (process.env.ADMIN_ROLE_NAMES || 'admin').split(
',',
);

options.session.user.isAdmin =
roles.length > 0 && adminRoleNames.some((role) => roles.includes(role));
if (options.session.user) {
options.session.user.isAdmin = isAdmin;
}

return options.session;
Expand Down

0 comments on commit 4993760

Please sign in to comment.