export default function AuthProvider({ children }) {
const { checkAuthentication, setUser } = useAuth();
useEffect(() => {
checkAuthentication();
const {
data: { subscription },
} = supabase.auth.onAuthStateChange(async (event) => {
switch (event) {
case 'SIGNED_IN':
if (window.location.pathname.includes('/auth/')) {
window.location.href = '/';
}
break;
case 'SIGNED_OUT':
if (!window.location.pathname.includes('/auth/')) {
window.location.href = '/auth/login';
}
break;
case 'USER_UPDATED':
// Only update user data if we're not on the set-password page
// to avoid interfering with the password update flow
if (!window.location.pathname.includes('/auth/set/password')) {
try {
const updatedUser = await getCurrentUser();
if (updatedUser) {
setUser(updatedUser);
}
} catch (error) {
console.error('Error fetching updated user data:', error);
}
}
break;
case 'PASSWORD_RECOVERY':
sessionStorage.setItem('isRecovery', 'true');
break;
}
});
return () => subscription.unsubscribe();
}, [checkAuthentication]);
return children;
}
export default function AuthProvider({ children }) {
const { checkAuthentication, setUser } = useAuth();
useEffect(() => {
checkAuthentication();
const {
data: { subscription },
} = supabase.auth.onAuthStateChange(async (event) => {
switch (event) {
case 'SIGNED_IN':
if (window.location.pathname.includes('/auth/')) {
window.location.href = '/';
}
break;
case 'SIGNED_OUT':
if (!window.location.pathname.includes('/auth/')) {
window.location.href = '/auth/login';
}
break;
case 'USER_UPDATED':
// Only update user data if we're not on the set-password page
// to avoid interfering with the password update flow
if (!window.location.pathname.includes('/auth/set/password')) {
try {
const updatedUser = await getCurrentUser();
if (updatedUser) {
setUser(updatedUser);
}
} catch (error) {
console.error('Error fetching updated user data:', error);
}
}
break;
case 'PASSWORD_RECOVERY':
sessionStorage.setItem('isRecovery', 'true');
break;
}
});
return () => subscription.unsubscribe();
}, [checkAuthentication]);
return children;
}