A beautiful Tangled client with Catppuccin colors. snarled.at
catppuccin atproto tangled
Something went wrong. Try again.
1.4 kB · 57 lines
TSX
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import { useEffect, useState, type ReactNode } from 'react'import { restoreSession, signOut as endSession,} from '../../lib/auth/oauth'import type { Session } from '@atcute/oauth-browser-client'import { AuthContext } from './AuthContext'
export function AuthProvider({ children }: { children: ReactNode }) { const [session, setSession] = useState<Session | null>(null) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState<string | null>(null)
useEffect(() => { if (window.location.pathname === '/auth/callback') { setIsLoading(false) return }
let active = true
restoreSession() .then((restoredSession) => { if (active) setSession(restoredSession) }) .catch(() => { if (active) setError('Could not restore your session.') }) .finally(() => { if (active) setIsLoading(false) })
return () => { active = false } }, [])
async function signOut(): Promise<void> { if (session === null) return
setError(null)
try { await endSession(session) } catch { setError('Signed out locally, but could not revoke the session with the provider.') } finally { setSession(null) } }
return ( <AuthContext.Provider value={{ session, isLoading, error, setSession, signOut }}> {children} </AuthContext.Provider> )}