fix: auto switch to expected org (#522)

This commit is contained in:
MaheshtheDev 2025-10-25 23:27:12 +00:00
parent 9036516c7d
commit 3ca82e7116

View file

@ -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={{