import React from 'react'; import type { UserProfile } from '~/common/user.ts' import { subscribeToAuthState } from './api/auth.ts' type AuthState = | {loginState: 'pending'} | {loginState: 'logged-out'} | {loginState: 'logged-in'; profile: UserProfile} type UserHandle = AuthState const AuthContext = React.createContext(null) export function useAuth(): UserHandle { const value = React.useContext(AuthContext) if (value === null) throw new Error('useAuth must be used inside an AuthProvider') return value } // TODO: UPDATE HERE TO ENABLE AUTH const initialAuthState: AuthState = {loginState: 'pending'} interface AuthProviderProps { children: React.ReactNode } /** * Handles authentication and token storage. * * Attaches a provider with login and logout callbacks. Handles authenticating using * a token found in the device keychain and attaching/removing the token on login/logout. * * @props children */ export function AuthProvider({children}: AuthProviderProps): JSX.Element { const [authState, setAuthState] = React.useState(initialAuthState) React.useEffect(() => { subscribeToAuthState((user) => { if (user) { setAuthState({ loginState: 'logged-in', profile: {displayName: user.displayName.split(' ')[0]}, }) } else setAuthState({ loginState: 'logged-out' }) }) }, []) return ( {children} ) } const UserContext = React.createContext(null) export function useUser(): UserProfile { const value = React.useContext(UserContext) if (value === null) throw new Error('useUser must be used inside a UserProvider') return value } interface UserProviderProps { profile: UserProfile children: React.ReactNode } export function UserProvider({children, profile}: UserProviderProps) { return ( {children} ) }