Create professional demos and 3D mockups in seconds, directly in your browser
"use client";
import {
createContext,
useContext,
useEffect,
useState,
useCallback,
useMemo,
} from "react";
import { createClient } from "@/utils/supabase/client";
import type { User, Session, AuthChangeEvent } from "@supabase/supabase-js";
export interface UserProfile {
id: string;
email: string;
full_name: string | null;
first_name: string | null;
last_name: string | null;
avatar_url: string | null;
provider: string;
theme: "light" | "dark" | "system";
created_at: string;
updated_at: string;
}
interface AuthContextType {
user: User | null;
profile: UserProfile | null;
session: Session | null;
loading: boolean;
signOut: () => Promise<void>;
refreshProfile: () => Promise<void>;
}
const AuthContext = createContext<AuthContextType | undefined>(undefined);
const PROFILE_REFRESH_EVENTS = new Set<AuthChangeEvent>([
"INITIAL_SESSION",
"SIGNED_IN",
"USER_UPDATED",
"PASSWORD_RECOVERY",
]);
const guestAuth: AuthContextType = {
user: null,
profile: null,
session: null,
loading: false,
signOut: async () => {},
refreshProfile: async () => {},
};
export function AuthProvider({ children, disabled = false }: {
children: React.ReactNode;
disabled?: boolean;
}) {
if (disabled) {
return <AuthContext.Provider value={guestAuth}>{children}</AuthContext.Provider>;
}
return <SessionAuthProvider>{children}</SessionAuthProvider>;
}
function SessionAuthProvider({ children }: { children: React.ReactNode }) {
const [user, setUser] = useState<User | null>(null);
const [profile, setProfile] = useState<UserProfile | null>(null);
const [session, setSession] = useState<Session | null>(null);
const [loading, setLoading] = useState(true);
const supabase = useMemo(() => createClient(), []);
const fetchProfile = useCallback(
async (userId: string) => {
try {
const { data, error } = await supabase
.from("user_profiles")
.select("*")
.eq("id", userId)
.single();
if (error) {
console.error("Error fetching profile:", error);
return null;
}
return data as UserProfile;
} catch (error) {
console.error("Error fetching profile:", error);
return null;
}
},
[supabase],
);
const refreshProfile = useCallback(async () => {
if (!user) return;
const profileData = await fetchProfile(user.id);
setProfile(profileData);
}, [user, fetchProfile]);
useEffect(() => {
let mounted = true;
const {
data: { subscription },
} = supabase.auth.onAuthStateChange(
(event: AuthChangeEvent, newSession: Session | null) => {
if (!mounted) return;
setSession(newSession);
setUser(newSession?.user ?? null);
setLoading(false);
if (event === "TOKEN_REFRESHED") {
return;
}
if (event === "SIGNED_OUT" || !newSession?.user) {
setProfile(null);
return;
}
if (PROFILE_REFRESH_EVENTS.has(event)) {
const userId = newSession.user.id;
void fetchProfile(userId).then((profileData) => {
if (mounted) setProfile(profileData);
});
}
},
);
return () => {
mounted = false;
subscription.unsubscribe();
};
}, [supabase, fetchProfile]);
const signOut = useCallback(async () => {
// Optimistic clear for snappy UI; SIGNED_OUT confirms via the listener.
setUser(null);
setProfile(null);
setSession(null);
const { error } = await supabase.auth.signOut({ scope: "local" });
if (error) {
console.error("Error signing out:", error);
}
}, [supabase]);
const value = useMemo(
() => ({ user, profile, session, loading, signOut, refreshProfile }),
[user, profile, session, loading, signOut, refreshProfile],
);
return (
<AuthContext.Provider value={value}>{children}</AuthContext.Provider>
);
}
export function useAuth() {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error("useAuth must be used within an AuthProvider");
}
return context;
}