diff options
| author | MaheshtheDev <[email protected]> | 2025-10-25 23:27:12 +0000 |
|---|---|---|
| committer | MaheshtheDev <[email protected]> | 2025-10-25 23:27:12 +0000 |
| commit | 3ca82e7116efceef1cccbfd294ad00efd659d5e5 (patch) | |
| tree | 09e07e55015546a9a22611ab1b058a12eaf6077d /packages | |
| parent | fix: bun lock build issue (#519) (diff) | |
| download | supermemory-3ca82e7116efceef1cccbfd294ad00efd659d5e5.tar.xz supermemory-3ca82e7116efceef1cccbfd294ad00efd659d5e5.zip | |
fix: auto switch to expected org (#522)
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/lib/auth-context.tsx | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/packages/lib/auth-context.tsx b/packages/lib/auth-context.tsx index 66ff84bc..a8a296c7 100644 --- a/packages/lib/auth-context.tsx +++ b/packages/lib/auth-context.tsx @@ -24,14 +24,32 @@ const AuthContext = createContext<AuthContextType | undefined>(undefined) export function AuthProvider({ children }: { children: ReactNode }) { const { data: session } = useSession() const [org, setOrg] = useState<Organization | null>(null) + const { data: orgs } = authClient.useListOrganizations() + const setActiveOrg = async (slug: string) => { + if (!slug) return + + const activeOrg = await authClient.organization.setActive({ + organizationSlug: slug, + }) + setOrg(activeOrg) + } + + // biome-ignore lint/correctness/useExhaustiveDependencies: ignoring the setActiveOrg dependency useEffect(() => { if (session?.session.activeOrganizationId) { authClient.organization.getFullOrganization().then((org) => { - setOrg(org) + if (org.metadata?.isConsumer === true) { + setOrg(org) + } else { + const consumerOrg = orgs?.find((o) => o.metadata?.isConsumer === true) + if (consumerOrg) { + setActiveOrg(consumerOrg.slug) + } + } }) } - }, [session?.session.activeOrganizationId]) + }, [session?.session.activeOrganizationId, orgs]) // When a session exists and there is a pending login method recorded, // promote it to the last-used method (successful login) and clear pending. @@ -53,29 +71,17 @@ export function AuthProvider({ children }: { children: ReactNode }) { const isFresh = Number.isFinite(ts) && now - ts < 10 * 60 * 1000 // 10 minutes TTL if (isFresh) { - localStorage.setItem( - "supermemory-last-login-method", - pendingMethod, - ) + localStorage.setItem("supermemory-last-login-method", pendingMethod) } } - } catch { } + } catch {} // Always clear pending markers once a session is present try { localStorage.removeItem("supermemory-pending-login-method") localStorage.removeItem("supermemory-pending-login-timestamp") - } catch { } + } catch {} }, [session?.session]) - const setActiveOrg = async (slug: string) => { - if (!slug) return - - const activeOrg = await authClient.organization.setActive({ - organizationSlug: slug, - }) - setOrg(activeOrg) - } - return ( <AuthContext.Provider value={{ |