From 3ecc9319c734e86d35acba0c8e7c977cf5283da5 Mon Sep 17 00:00:00 2001 From: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com> Date: Tue, 25 Mar 2025 09:46:00 +0530 Subject: [PATCH] fix: Introduce a new prop in the session to extract the profile username when inside an org (#18979) * fix: flaky test organization.spec * fix: use profile.username * update * update * update --- packages/features/auth/lib/getServerSession.ts | 1 + packages/features/auth/lib/next-auth-options.ts | 4 ++++ packages/features/shell/SideBar.tsx | 2 +- packages/types/next-auth.d.ts | 2 ++ 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/features/auth/lib/getServerSession.ts b/packages/features/auth/lib/getServerSession.ts index b5df23fca1..447a54dc13 100644 --- a/packages/features/auth/lib/getServerSession.ts +++ b/packages/features/auth/lib/getServerSession.ts @@ -99,6 +99,7 @@ export async function getServerSession(options: { }), belongsToActiveTeam: token.belongsToActiveTeam, org: token.org, + orgAwareUsername: token.orgAwareUsername, locale: user.locale ?? undefined, profile: user.profile, }, diff --git a/packages/features/auth/lib/next-auth-options.ts b/packages/features/auth/lib/next-auth-options.ts index be14526ce8..b15c531570 100644 --- a/packages/features/auth/lib/next-auth-options.ts +++ b/packages/features/auth/lib/next-auth-options.ts @@ -546,6 +546,7 @@ export const getOptions = ({ profileId: profile.id, upId, belongsToActiveTeam, + orgAwareUsername: profileOrg ? profile.username : existingUser.username, // All organizations in the token would be too big to store. It breaks the sessions request. // So, we just set the currently switched organization only here. // platform org user don't need profiles nor domains @@ -580,6 +581,7 @@ export const getOptions = ({ id: user.id, name: user.name, username: user.username, + orgAwareUsername: user?.org ? user.profile?.username : user.username, email: user.email, role: user.role, impersonatedBy: user.impersonatedBy, @@ -684,6 +686,7 @@ export const getOptions = ({ impersonatedBy: token.impersonatedBy, belongsToActiveTeam: token?.belongsToActiveTeam as boolean, org: token?.org, + orgAwareUsername: token.orgAwareUsername, locale: existingUser.locale, } as JWT; } @@ -709,6 +712,7 @@ export const getOptions = ({ id: token.id as number, name: token.name, username: token.username as string, + orgAwareUsername: token.orgAwareUsername, role: token.role as UserPermissionRole, impersonatedBy: token.impersonatedBy, belongsToActiveTeam: token?.belongsToActiveTeam as boolean, diff --git a/packages/features/shell/SideBar.tsx b/packages/features/shell/SideBar.tsx index 396dc10606..00d3a3ec41 100644 --- a/packages/features/shell/SideBar.tsx +++ b/packages/features/shell/SideBar.tsx @@ -57,7 +57,7 @@ export function SideBar({ bannersHeight, user }: SideBarProps) { const isPlatformPages = pathname?.startsWith("/settings/platform"); const isAdmin = session.data?.user.role === UserPermissionRole.ADMIN; - const publicPageUrl = `${getBookerBaseUrlSync(user?.org?.slug ?? null)}/${user?.username}`; + const publicPageUrl = `${getBookerBaseUrlSync(user?.org?.slug ?? null)}/${user?.orgAwareUsername}`; const bottomNavItems = useBottomNavItems({ publicPageUrl, diff --git a/packages/types/next-auth.d.ts b/packages/types/next-auth.d.ts index fadae41d4a..487cc5f3bb 100644 --- a/packages/types/next-auth.d.ts +++ b/packages/types/next-auth.d.ts @@ -35,6 +35,7 @@ declare module "next-auth" { role: MembershipRole; }; username?: PrismaUser["username"]; + orgAwareUsername?: PrismaUser["username"]; avatarUrl?: PrismaUser["avatarUrl"]; role?: PrismaUser["role"] | "INACTIVE_ADMIN"; locale?: string | null; @@ -66,6 +67,7 @@ declare module "next-auth/jwt" { domainSuffix: string; role: MembershipRole; }; + orgAwareUsername?: PrismaUser["username"]; organizationId?: number | null; locale?: string; }