diff --git a/frontend/src/auth/AuthProvider.tsx b/frontend/src/auth/AuthProvider.tsx new file mode 100644 index 0000000..41a95c3 --- /dev/null +++ b/frontend/src/auth/AuthProvider.tsx @@ -0,0 +1,31 @@ +import { createContext, useContext, useEffect, useState, type ReactNode } from 'react'; +import type { Session } from '@supabase/supabase-js'; +import { supabase } from '../lib/supabase'; + +type AuthContextValue = { + session: Session | null; + loading: boolean; +}; + +const AuthContext = createContext({ session: null, loading: true }); + +export function AuthProvider({ children }: { children: ReactNode }) { + const [session, setSession] = useState(null); + const [loading, setLoading] = useState(true); + + useEffect(() => { + const { data } = supabase.auth.onAuthStateChange((_event, s) => { + setSession(s); + setLoading(false); + }); + return () => { + data.subscription.unsubscribe(); + }; + }, []); + + return {children}; +} + +export function useAuth() { + return useContext(AuthContext); +}