aboutsummaryrefslogtreecommitdiff
path: root/packages/lib/auth-context.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'packages/lib/auth-context.tsx')
-rw-r--r--packages/lib/auth-context.tsx77
1 files changed, 38 insertions, 39 deletions
diff --git a/packages/lib/auth-context.tsx b/packages/lib/auth-context.tsx
index 66ff84bc..a88c73c8 100644
--- a/packages/lib/auth-context.tsx
+++ b/packages/lib/auth-context.tsx
@@ -1,4 +1,4 @@
-"use client"
+"use client";
import {
createContext,
@@ -6,75 +6,74 @@ import {
useContext,
useEffect,
useState,
-} from "react"
-import { authClient, useSession } from "./auth"
+} from "react";
+import { authClient, useSession } from "./auth";
-type Organization = typeof authClient.$Infer.ActiveOrganization
-type SessionData = NonNullable<ReturnType<typeof useSession>["data"]>
+type Organization = typeof authClient.$Infer.ActiveOrganization;
+type SessionData = NonNullable<ReturnType<typeof useSession>["data"]>;
interface AuthContextType {
- session: SessionData["session"] | null
- user: SessionData["user"] | null
- org: Organization | null
- setActiveOrg: (orgSlug: string) => Promise<void>
+ session: SessionData["session"] | null;
+ user: SessionData["user"] | null;
+ org: Organization | null;
+ setActiveOrg: (orgSlug: string) => Promise<void>;
}
-const AuthContext = createContext<AuthContextType | undefined>(undefined)
+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: session } = useSession();
+ const [org, setOrg] = useState<Organization | null>(null);
useEffect(() => {
if (session?.session.activeOrganizationId) {
authClient.organization.getFullOrganization().then((org) => {
- setOrg(org)
- })
+ setOrg(org);
+ });
}
- }, [session?.session.activeOrganizationId])
+ }, [session?.session.activeOrganizationId]);
// When a session exists and there is a pending login method recorded,
// promote it to the last-used method (successful login) and clear pending.
useEffect(() => {
- if (typeof window === "undefined") return
- if (!session?.session) return
+ if (typeof window === "undefined") return;
+ if (!session?.session) return;
try {
const pendingMethod = localStorage.getItem(
"supermemory-pending-login-method",
- )
+ );
const pendingTsRaw = localStorage.getItem(
"supermemory-pending-login-timestamp",
- )
+ );
if (pendingMethod) {
- const now = Date.now()
- const ts = pendingTsRaw ? Number.parseInt(pendingTsRaw, 10) : NaN
- const isFresh = Number.isFinite(ts) && now - ts < 10 * 60 * 1000 // 10 minutes TTL
+ const now = Date.now();
+ const ts = pendingTsRaw
+ ? Number.parseInt(pendingTsRaw, 10)
+ : Number.NaN;
+ 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 { }
- }, [session?.session])
+ localStorage.removeItem("supermemory-pending-login-method");
+ localStorage.removeItem("supermemory-pending-login-timestamp");
+ } catch {}
+ }, [session?.session]);
const setActiveOrg = async (slug: string) => {
- if (!slug) return
+ if (!slug) return;
const activeOrg = await authClient.organization.setActive({
organizationSlug: slug,
- })
- setOrg(activeOrg)
- }
+ });
+ setOrg(activeOrg);
+ };
return (
<AuthContext.Provider
@@ -87,13 +86,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
>
{children}
</AuthContext.Provider>
- )
+ );
}
export function useAuth() {
- const context = useContext(AuthContext)
+ const context = useContext(AuthContext);
if (context === undefined) {
- throw new Error("useAuth must be used within an AuthProvider")
+ throw new Error("useAuth must be used within an AuthProvider");
}
- return context
+ return context;
}