Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot] 2026-04-17 17:02:12 +00:00
parent 0eb8689a27
commit fa8bee0e36
7 changed files with 271 additions and 0 deletions

34
src/hooks/use-auth.ts Normal file
View file

@ -0,0 +1,34 @@
"use client";
import { useEffect, useState } from "react";
import type { Session, User } from "@supabase/supabase-js";
import { supabase } from "@/integrations/supabase/client";
interface AuthState {
user: User | null;
session: Session | null;
loading: boolean;
}
export function useAuth(): AuthState {
const [state, setState] = useState<AuthState>({
user: null,
session: null,
loading: true,
});
useEffect(() => {
// CRITICAL: subscribe BEFORE getSession (avoid missed events)
const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, session) => {
setState({ user: session?.user ?? null, session, loading: false });
});
supabase.auth.getSession().then(({ data: { session } }) => {
setState({ user: session?.user ?? null, session, loading: false });
});
return () => subscription.unsubscribe();
}, []);
return state;
}