Add AuthProvider tracking Supabase session

This commit is contained in:
EugeneTes
2026-08-15 11:53:22 +00:00
parent 22240c89e8
commit 676b3b3a86

View File

@@ -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<AuthContextValue>({ session: null, loading: true });
export function AuthProvider({ children }: { children: ReactNode }) {
const [session, setSession] = useState<Session | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
const { data } = supabase.auth.onAuthStateChange((_event, s) => {
setSession(s);
setLoading(false);
});
return () => {
data.subscription.unsubscribe();
};
}, []);
return <AuthContext.Provider value={{ session, loading }}>{children}</AuthContext.Provider>;
}
export function useAuth() {
return useContext(AuthContext);
}