diff --git a/packages/lib/auth-context.tsx b/packages/lib/auth-context.tsx index 84b095b0..d3d0bedb 100644 --- a/packages/lib/auth-context.tsx +++ b/packages/lib/auth-context.tsx @@ -111,6 +111,11 @@ export function AuthProvider({ children }: { children: ReactNode }) { const run = async () => { try { + // OAuth consent owns org selection for the authorization transaction. + const shouldRestoreSavedOrg = + typeof window === "undefined" || + window.location.pathname !== "/oauth/consent" + if (orgs.length === 0) { if (!cancelled) setOrg(null) return @@ -130,19 +135,21 @@ export function AuthProvider({ children }: { children: ReactNode }) { return } - const savedSlug = localStorage.getItem(STORAGE_KEY) - if (savedSlug) { - const match = orgs.find((o) => o.slug === savedSlug) - if (match) { - if (activeOrgId === match.id) { - const full = await authClient.organization.getFullOrganization() - if (!cancelled) setOrg(full?.data ?? null) - } else { - await setActiveOrg(savedSlug) + if (shouldRestoreSavedOrg) { + const savedSlug = localStorage.getItem(STORAGE_KEY) + if (savedSlug) { + const match = orgs.find((o) => o.slug === savedSlug) + if (match) { + if (activeOrgId === match.id) { + const full = await authClient.organization.getFullOrganization() + if (!cancelled) setOrg(full?.data ?? null) + } else { + await setActiveOrg(savedSlug) + } + return } - return + localStorage.removeItem(STORAGE_KEY) } - localStorage.removeItem(STORAGE_KEY) } if (activeOrgId) {