From 5cff767e32e9c09433f431cd9d702be0e27bb721 Mon Sep 17 00:00:00 2001 From: Usman Baig Date: Thu, 26 Mar 2026 23:59:30 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20org=20switch=20now=20updates=20auth=20co?= =?UTF-8?q?ntext=20immediately=20=E2=80=94=20no=20stale=20org=20in=20heade?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/layout-content.tsx | 10 ++++------ lib/auth/context.tsx | 11 +++-------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/app/layout-content.tsx b/app/layout-content.tsx index 3e0d959..745e0e9 100644 --- a/app/layout-content.tsx +++ b/app/layout-content.tsx @@ -81,12 +81,10 @@ function LayoutInner({ children }: { children: React.ReactNode }) { setIsSwitchingOrg(true) const { access_token } = await switchContext(orgId) await setSessionAction(access_token) - router.refresh() - // Allow time for server components to re-fetch with new auth - setTimeout(() => { - setIsSwitchingOrg(false) - router.push('/') - }, 600) + // Refresh auth context (re-fetches /auth/user/me with new JWT, updates org_id + SWR cache) + await auth.refresh() + router.push('/') + setTimeout(() => setIsSwitchingOrg(false), 300) } catch (err) { setIsSwitchingOrg(false) logger.error('Failed to switch organization', err) diff --git a/lib/auth/context.tsx b/lib/auth/context.tsx index ef5f7af..4ce4881 100644 --- a/lib/auth/context.tsx +++ b/lib/auth/context.tsx @@ -112,14 +112,9 @@ export function AuthProvider({ children }: { children: React.ReactNode }) { try { const userData = await apiRequest('/auth/user/me') - setUser(prev => { - const merged = { - ...userData, - org_id: prev?.org_id, - role: prev?.role - } - localStorage.setItem('user', JSON.stringify(merged)) - return merged + setUser(() => { + localStorage.setItem('user', JSON.stringify(userData)) + return userData }) } catch (e) { logger.error('Failed to refresh user data', e)