We finally fixed the sign in and sign out stuff with our client components not updating

This commit is contained in:
2025-05-30 11:19:22 -05:00
parent 28569c4f4e
commit 969e101d21
5 changed files with 89 additions and 64 deletions

View File

@@ -1,8 +1,23 @@
'use client';
import React, { createContext, useContext, useState, useEffect, type ReactNode } from 'react';
import { getUser, getProfile, updateProfile as updateProfileAction, getSignedUrl } from '@/lib/actions';
import { type User, type Profile, createClient } from '@/utils/supabase';
import React, {
type ReactNode,
createContext,
useContext,
useEffect,
useState,
} from 'react';
import {
getProfile,
getSignedUrl,
getUser,
updateProfile as updateProfileAction,
} from '@/lib/actions';
import {
type User,
type Profile,
createClient,
} from '@/utils/supabase';
import { toast } from 'sonner';
type AuthContextType = {
@@ -41,30 +56,29 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
}
setUser(userResponse.data);
// Get profile data
const profileResponse = await getProfile();
if (!profileResponse.success) {
setProfile(null);
setAvatarUrl(null);
return;
}
setProfile(profileResponse.data);
// Get avatar URL if available
if (profileResponse.data.avatar_url) {
const avatarResponse = await getSignedUrl({
bucket: 'avatars',
url: profileResponse.data.avatar_url,
});
if (avatarResponse.success) {
setAvatarUrl(avatarResponse.data);
}
}
} catch (error) {
console.error('Error fetching user data:', error);
toast.error('Failed to load user data');
} finally {
setIsLoading(false);
@@ -73,8 +87,9 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
useEffect(() => {
const supabase = createClient();
fetchUserData().catch((error) => {
console.error('Error fetching user data:', error);
console.error('💥 Initial fetch error:', error);
});
const {
@@ -90,13 +105,8 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
}
});
const intervalId = setInterval(() => {
void fetchUserData();
}, 1 * 60 * 1000);
return () => {
subscription.unsubscribe();
clearInterval(intervalId);
};
}, []);
@@ -140,6 +150,7 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
};
const refreshUserData = async () => {
console.log('Manual refresh triggered');
await fetchUserData();
};

View File

@@ -13,14 +13,20 @@ import {
DropdownMenuTrigger,
} from '@/components/ui';
import { useAuth } from '@/components/context/auth';
import { useRouter } from 'next/navigation';
import { signOut } from '@/lib/actions';
import { User } from 'lucide-react';
const AvatarDropdown = () => {
const { profile, avatarUrl, isLoading } = useAuth();
const { profile, avatarUrl, isLoading, refreshUserData } = useAuth();
const router = useRouter();
const handleSignOut = async () => {
await signOut();
const result = await signOut();
if (result?.success) {
await refreshUserData();
router.push('/sign-in');
}
};
const getInitials = (name: string | null | undefined): string => {