Working on Auth flows. Struggling with getting client and server to both update when signing in or out.

This commit is contained in:
2025-05-30 10:06:34 -05:00
parent 22cf7be870
commit 28569c4f4e
11 changed files with 344 additions and 223 deletions

View File

@ -2,7 +2,7 @@
import React, { createContext, useContext, useState, useEffect, type ReactNode } from 'react';
import { getUser, getProfile, updateProfile as updateProfileAction, getSignedUrl } from '@/lib/actions';
import type { User, Profile } from '@/utils/supabase';
import { type User, type Profile, createClient } from '@/utils/supabase';
import { toast } from 'sonner';
type AuthContextType = {
@ -72,15 +72,32 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
};
useEffect(() => {
const supabase = createClient();
fetchUserData().catch((error) => {
console.error('Error fetching user data:', error);
});
const {
data: { subscription },
} = supabase.auth.onAuthStateChange(async (event, session) => {
if (event === 'SIGNED_IN' || event === 'TOKEN_REFRESHED') {
await fetchUserData();
} else if (event === 'SIGNED_OUT') {
setUser(null);
setProfile(null);
setAvatarUrl(null);
setIsLoading(false);
}
});
const intervalId = setInterval(() => {
void fetchUserData();
}, 30 * 60 * 1000);
}, 1 * 60 * 1000);
return () => clearInterval(intervalId);
return () => {
subscription.unsubscribe();
clearInterval(intervalId);
};
}, []);
const updateProfile = async (data: {