I have supabase authentex.js file import React, { createContext, useContext, useEffect, useState, useCallback, useMemo } from 'react'; import { supabase } from '@/lib/customSupabaseClient'; import { useToast } from '@/components/ui/use-toast'; const AuthContext = createContext(undefined); export const AuthProvider = ({ children }) => { const { toast } = useToast(); const [user, setUser] = useState(null); const [profile, setProfile] = useState(null); const [session, setSession] = useState(null); const [loading, setLoading] = useState(true); const fetchProfile = useCallback(async (userId) => { const { data, error } = await supabase .from('profiles') .select('*') .eq('id', userId) .single(); if (error && error.code !== 'PGRST116') { // Ignore 'exact one row' error if profile is missing console.error("Error fetching profile:", error); return null; } return data; }, []); const handleSession = useCallback(async (session) => { setSession(session); const currentUser = session?.user ?? null; setUser(currentUser); if (currentUser) { const userProfile = await fetchProfile(currentUser.id); setProfile(userProfile); } else { setProfile(null); } setLoading(false); }, [fetchProfile]); useEffect(() => { const getSession = async () => { const { data: { session } } = await supabase.auth.getSession(); await handleSession(session); }; getSession(); const { data: { subscription } } = supabase.auth.onAuthStateChange( async (event, session) => { await handleSession(session); } ); return () => subscription.unsubscribe(); }, [handleSession]); const signUp = useCallback(async (email, password, options) => { const { error } = await supabase.auth.signUp({ email, password, options, }); if (error) { toast({ variant: "destructive", title: "Sign up Failed", description: error.message || "Something went wrong", }); } else { toast({ title: "Success!", description: "Please check your email for a confirmation link." }); } return { error }; }, [toast]); const signIn = useCallback(async (email, password) => { const { error } = await supabase.auth.signInWithPassword({ email, password, }); if (error) { toast({ variant: "destructive", title: "Sign in Failed", description: error.message || "Something went wrong", }); } return { error }; }, [toast]); const signOut = useCallback(async () => { const { error } = await supabase.auth.signOut(); if (error) { toast({ variant: "destructive", title: "Sign out Failed", description: error.message || "Something went wrong", }); } return { error }; }, [toast]); const value = useMemo(() => ({ user, profile, session, loading, isAdmin: profile?.role === 'admin', signUp, signIn, signOut, }), [user, profile, session, loading, signUp, signIn, signOut]); return {children}; }; export const useAuth = () => { const context = useContext(AuthContext); if (context === undefined) { throw new Error('useAuth must be used within an AuthProvider'); } return context; };