Spaces:
Running
Running
Download web/src/lib/auth.tsx from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 2.77 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/lib/auth.tsx
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/web/src/lib/auth.tsx
-
curl -L -o auth.tsx https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/lib/auth.tsx
2.77 kB
| "use client"; | |
| import { | |
| createContext, | |
| useCallback, | |
| useContext, | |
| useEffect, | |
| useMemo, | |
| useState, | |
| type ReactNode, | |
| } from "react"; | |
| import { api } from "@/lib/api"; | |
| import * as session from "@/lib/session"; | |
| import type { Me, Role } from "@/lib/types"; | |
| type Status = "loading" | "anonymous" | "authenticated"; | |
| interface AuthValue { | |
| status: Status; | |
| me: Me | null; | |
| login: (email: string, password: string) => Promise<void>; | |
| signup: (business: string, email: string, password: string) => Promise<void>; | |
| logout: () => Promise<void>; | |
| switchTenant: (tenantId: string) => Promise<void>; | |
| reload: () => Promise<void>; | |
| hasRole: (min: Role) => boolean; | |
| } | |
| export const ROLE_RANK: Record<Role, number> = { | |
| viewer: 0, | |
| agent_operator: 1, | |
| manager: 2, | |
| owner: 3, | |
| }; | |
| const AuthContext = createContext<AuthValue | null>(null); | |
| export function AuthProvider({ children }: { children: ReactNode }) { | |
| const [status, setStatus] = useState<Status>("loading"); | |
| const [me, setMe] = useState<Me | null>(null); | |
| const loadMe = useCallback(async () => { | |
| try { | |
| const m = await api<Me>("/me"); | |
| setMe(m); | |
| setStatus("authenticated"); | |
| } catch { | |
| setMe(null); | |
| setStatus("anonymous"); | |
| } | |
| }, []); | |
| useEffect(() => { | |
| let alive = true; | |
| // A reload has no access token in memory: trade the httpOnly refresh cookie for one. | |
| session.refreshAccessToken().then((token) => { | |
| if (!alive) return; | |
| if (token) void loadMe(); | |
| else setStatus("anonymous"); | |
| }); | |
| const off = session.onSessionExpired(() => { | |
| setMe(null); | |
| setStatus("anonymous"); | |
| }); | |
| return () => { | |
| alive = false; | |
| off(); | |
| }; | |
| }, [loadMe]); | |
| const value = useMemo<AuthValue>( | |
| () => ({ | |
| status, | |
| me, | |
| login: async (email, password) => { | |
| await session.login(email, password); | |
| await loadMe(); | |
| }, | |
| signup: async (business, email, password) => { | |
| await session.signup(business, email, password); | |
| await loadMe(); | |
| }, | |
| logout: async () => { | |
| await session.logout(); | |
| setMe(null); | |
| setStatus("anonymous"); | |
| }, | |
| switchTenant: async (tenantId) => { | |
| await session.switchTenant(tenantId); | |
| await loadMe(); | |
| }, | |
| reload: loadMe, | |
| hasRole: (min) => (me ? ROLE_RANK[me.role] >= ROLE_RANK[min] : false), | |
| }), | |
| [status, me, loadMe], | |
| ); | |
| return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; | |
| } | |
| export function useAuth(): AuthValue { | |
| const v = useContext(AuthContext); | |
| if (!v) throw new Error("useAuth outside AuthProvider"); | |
| return v; | |
| } | |