Download frontend/src/context/AuthContext.jsx from Seth0330/qualifier: direct link, hf CLI and curl.
- Browser
- Download file 1.77 kB
-
https://huggingface.co/spaces/Seth0330/qualifier/resolve/main/frontend/src/context/AuthContext.jsx
- Command line
-
hf download hf://spaces/Seth0330/qualifier/frontend/src/context/AuthContext.jsx
-
curl -L -o AuthContext.jsx https://huggingface.co/spaces/Seth0330/qualifier/resolve/main/frontend/src/context/AuthContext.jsx
1.77 kB
| import React, { | |
| createContext, | |
| useCallback, | |
| useContext, | |
| useEffect, | |
| useMemo, | |
| useState, | |
| } from 'react'; | |
| import { apiFetch } from '@/lib/api'; | |
| const AuthContext = createContext(null); | |
| export function AuthProvider({ children }) { | |
| const [phase, setPhase] = useState('loading'); | |
| const [googleOn, setGoogleOn] = useState(false); | |
| const [microsoftOn, setMicrosoftOn] = useState(false); | |
| const [user, setUser] = useState(null); | |
| const refresh = useCallback(async () => { | |
| try { | |
| const [st, meRes] = await Promise.all([ | |
| apiFetch('/api/auth/status').then((r) => r.json()), | |
| apiFetch('/api/auth/me'), | |
| ]); | |
| setGoogleOn(!!st.googleConfigured); | |
| setMicrosoftOn(!!st.microsoftConfigured); | |
| setUser(meRes.ok ? await meRes.json() : null); | |
| setPhase('ready'); | |
| } catch { | |
| setPhase('error'); | |
| } | |
| }, []); | |
| useEffect(() => { | |
| refresh(); | |
| }, [refresh]); | |
| useEffect(() => { | |
| const onAuth = () => refresh(); | |
| window.addEventListener('emailout-auth-changed', onAuth); | |
| return () => window.removeEventListener('emailout-auth-changed', onAuth); | |
| }, [refresh]); | |
| const value = useMemo( | |
| () => ({ | |
| phase, | |
| googleOn, | |
| microsoftOn, | |
| user, | |
| signedIn: !!user, | |
| refresh, | |
| }), | |
| [phase, googleOn, microsoftOn, user, refresh] | |
| ); | |
| return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; | |
| } | |
| export function useAuth() { | |
| const ctx = useContext(AuthContext); | |
| if (!ctx) { | |
| throw new Error('useAuth must be used within AuthProvider'); | |
| } | |
| return ctx; | |
| } | |