Spaces:
Sleeping
Sleeping
Crs-Lionel
Claude Opus 4.8
Initial commit: FilezKit backend + frontend, dockerized for HF Spaces
82a25a8 Download frontend/src/lib/AuthContext.tsx from tech-ties/filezkit-backend: direct link, hf CLI and curl.
- Browser
- Download file 2.7 kB
-
https://huggingface.co/spaces/tech-ties/filezkit-backend/resolve/main/frontend/src/lib/AuthContext.tsx
- Command line
-
hf download hf://spaces/tech-ties/filezkit-backend/frontend/src/lib/AuthContext.tsx
-
curl -L -o AuthContext.tsx https://huggingface.co/spaces/tech-ties/filezkit-backend/resolve/main/frontend/src/lib/AuthContext.tsx
2.7 kB
| "use client"; | |
| import { | |
| createContext, | |
| useContext, | |
| useState, | |
| useEffect, | |
| useCallback, | |
| type ReactNode, | |
| } from "react"; | |
| import { | |
| loginUser, | |
| registerUser, | |
| googleLoginUser, | |
| getMe, | |
| type AuthUser, | |
| } from "./authApi"; | |
| const TOKEN_KEY = "pdfflow_token"; | |
| interface AuthContextType { | |
| user: AuthUser | null; | |
| token: string | null; | |
| isAuthenticated: boolean; | |
| isLoading: boolean; | |
| login: (email: string, password: string) => Promise<void>; | |
| loginWithGoogle: (googleAccessToken: string) => Promise<void>; | |
| register: (email: string, password: string) => Promise<AuthUser>; | |
| logout: () => void; | |
| } | |
| const AuthContext = createContext<AuthContextType | undefined>(undefined); | |
| export function AuthProvider({ children }: { children: ReactNode }) { | |
| const [user, setUser] = useState<AuthUser | null>(null); | |
| const [token, setToken] = useState<string | null>(null); | |
| const [isLoading, setIsLoading] = useState(true); | |
| // On mount, check for stored token and validate it | |
| useEffect(() => { | |
| const stored = localStorage.getItem(TOKEN_KEY); | |
| if (stored) { | |
| setToken(stored); | |
| getMe() | |
| .then((u) => setUser(u)) | |
| .catch(() => { | |
| // Token expired or invalid — clear it | |
| localStorage.removeItem(TOKEN_KEY); | |
| setToken(null); | |
| }) | |
| .finally(() => setIsLoading(false)); | |
| } else { | |
| setIsLoading(false); | |
| } | |
| }, []); | |
| const login = useCallback(async (email: string, password: string) => { | |
| const res = await loginUser(email, password); | |
| localStorage.setItem(TOKEN_KEY, res.access_token); | |
| setToken(res.access_token); | |
| const me = await getMe(); | |
| setUser(me); | |
| }, []); | |
| const loginWithGoogle = useCallback(async (googleAccessToken: string) => { | |
| const res = await googleLoginUser(googleAccessToken); | |
| localStorage.setItem(TOKEN_KEY, res.access_token); | |
| setToken(res.access_token); | |
| const me = await getMe(); | |
| setUser(me); | |
| }, []); | |
| const register = useCallback(async (email: string, password: string) => { | |
| const newUser = await registerUser(email, password); | |
| return newUser; | |
| }, []); | |
| const logout = useCallback(() => { | |
| localStorage.removeItem(TOKEN_KEY); | |
| setToken(null); | |
| setUser(null); | |
| }, []); | |
| return ( | |
| <AuthContext.Provider | |
| value={{ | |
| user, | |
| token, | |
| isAuthenticated: !!user && !!token, | |
| isLoading, | |
| login, | |
| loginWithGoogle, | |
| register, | |
| logout, | |
| }} | |
| > | |
| {children} | |
| </AuthContext.Provider> | |
| ); | |
| } | |
| export function useAuth() { | |
| const ctx = useContext(AuthContext); | |
| if (!ctx) { | |
| throw new Error("useAuth must be used within an AuthProvider"); | |
| } | |
| return ctx; | |
| } | |