MCPcopy Create free account
hub / github.com/0010aor/FlashNotes / AuthProvider

Function AuthProvider

frontend/src/hooks/useAuthContext.tsx:16–59  ·  view source on GitHub ↗
({ children }: { children: React.ReactNode })

Source from the content-addressed store, hash-verified

14const AuthContext = createContext<AuthContextType | undefined>(undefined)
15
16function AuthProvider({ children }: { children: React.ReactNode }) {
17 const [isGuest, setIsGuest] = useState(() => localStorage.getItem(GUEST_MODE_KEY) === 'true')
18 const [isLoggedIn, setIsLoggedIn] = useState(
19 () =>
20 Boolean(localStorage.getItem(ACCESS_TOKEN_KEY)) ||
21 localStorage.getItem(GUEST_MODE_KEY) === 'true',
22 )
23
24 useEffect(() => {
25 const handleStorage = (e: StorageEvent) => {
26 if (e.key === GUEST_MODE_KEY || e.key === ACCESS_TOKEN_KEY) {
27 setIsGuest(localStorage.getItem(GUEST_MODE_KEY) === 'true')
28 setIsLoggedIn(
29 Boolean(localStorage.getItem(ACCESS_TOKEN_KEY)) ||
30 localStorage.getItem(GUEST_MODE_KEY) === 'true',
31 )
32 }
33 }
34 window.addEventListener('storage', handleStorage)
35 return () => window.removeEventListener('storage', handleStorage)
36 }, [])
37
38 const setGuestMode = (value: boolean) => {
39 if (value) {
40 localStorage.setItem(GUEST_MODE_KEY, 'true')
41 } else {
42 localStorage.removeItem(GUEST_MODE_KEY)
43 }
44 setIsGuest(value)
45 setIsLoggedIn(value || Boolean(localStorage.getItem(ACCESS_TOKEN_KEY)))
46 }
47
48 const logout = () => {
49 localStorage.removeItem(ACCESS_TOKEN_KEY)
50 setGuestMode(false)
51 setIsLoggedIn(false)
52 }
53
54 return (
55 <AuthContext.Provider value={{ isGuest, isLoggedIn, setGuestMode, logout }}>
56 {children}
57 </AuthContext.Provider>
58 )
59}
60
61function useAuthContext() {
62 const ctx = useContext(AuthContext)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected