Spaces:
Runtime error
Runtime error
Download frontend/src/hooks/useAuth.tsx from firebird-technologies/NoteStack-Test: direct link, hf CLI and curl.
- Browser
- Download file 1.58 kB
-
https://huggingface.co/spaces/firebird-technologies/NoteStack-Test/resolve/1aa4a1663ff037b16e33cea96515f0268a23545d/frontend/src/hooks/useAuth.tsx
- Command line
-
hf download hf://spaces/firebird-technologies/NoteStack-Test@1aa4a1663ff037b16e33cea96515f0268a23545d/frontend/src/hooks/useAuth.tsx
-
curl -L -o useAuth.tsx https://huggingface.co/spaces/firebird-technologies/NoteStack-Test/resolve/1aa4a1663ff037b16e33cea96515f0268a23545d/frontend/src/hooks/useAuth.tsx
1.58 kB
| import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from "react"; | |
| import { authApi, type LoginResult, type User } from "../api/auth"; | |
| import { tokens } from "../api/client"; | |
| import { clearSourceCache } from "../components/video/sourceCache"; | |
| type AuthState = { | |
| user: User | null; | |
| loading: boolean; | |
| complete: (result: LoginResult) => void; | |
| logout: () => Promise<void>; | |
| }; | |
| const AuthContext = createContext<AuthState | null>(null); | |
| export function AuthProvider({ children }: { children: ReactNode }) { | |
| const [user, setUser] = useState<User | null>(null); | |
| const [loading, setLoading] = useState(Boolean(tokens.access)); | |
| useEffect(() => { | |
| if (!tokens.access) return; | |
| authApi | |
| .me() | |
| .then(setUser) | |
| .catch(() => tokens.clear()) | |
| .finally(() => setLoading(false)); | |
| }, []); | |
| const complete = useCallback((result: LoginResult) => { | |
| tokens.set(result.access_token, result.refresh_token); | |
| clearSourceCache(); | |
| setUser(result.user); | |
| }, []); | |
| const logout = useCallback(async () => { | |
| try { | |
| await authApi.logout(); | |
| } finally { | |
| tokens.clear(); | |
| clearSourceCache(); | |
| setUser(null); | |
| } | |
| }, []); | |
| const value = useMemo(() => ({ user, loading, complete, logout }), [user, loading, complete, logout]); | |
| return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; | |
| } | |
| export function useAuth(): AuthState { | |
| const ctx = useContext(AuthContext); | |
| if (!ctx) throw new Error("useAuth must be used inside AuthProvider"); | |
| return ctx; | |
| } | |