diff --git a/susconecta/app/(main-routes)/perfil/page.tsx b/susconecta/app/(main-routes)/perfil/page.tsx index 734aa6f..e5c7796 100644 --- a/susconecta/app/(main-routes)/perfil/page.tsx +++ b/susconecta/app/(main-routes)/perfil/page.tsx @@ -51,7 +51,7 @@ interface UserProfile { export default function PerfilPage() { const router = useRouter(); - const { user: authUser } = useAuth(); + const { user: authUser, updateUserProfile } = useAuth(); const [userInfo, setUserInfo] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); @@ -229,6 +229,31 @@ export default function PerfilPage() { } : null, } : null ); + + // Also update global auth profile so header/avatar updates immediately + try { + if (typeof updateUserProfile === 'function') { + updateUserProfile({ + // Persist common keys used across the app + foto_url: editingData.avatar_url || undefined, + telefone: editingData.phone || undefined + }); + } else { + // Fallback: try to persist directly to localStorage so next reload shows it + try { + const raw = localStorage.getItem('auth_user') + if (raw) { + const u = JSON.parse(raw) + u.profile = u.profile || {} + if (editingData.avatar_url) { u.profile.foto_url = editingData.avatar_url; u.profile.avatar_url = editingData.avatar_url } + if (editingData.phone) u.profile.telefone = editingData.phone + localStorage.setItem('auth_user', JSON.stringify(u)) + } + } catch (_e) {} + } + } catch (err) { + console.warn('[PERFIL] Falha ao sincronizar profile global:', err) + } } catch (err: any) { console.error('[PERFIL] Erro ao salvar:', err); } @@ -591,7 +616,25 @@ export default function PerfilPage() { setEditingData({...editingData, avatar_url: newUrl})} + onAvatarChange={(newUrl) => { + setEditingData({...editingData, avatar_url: newUrl}) + try { + if (typeof updateUserProfile === 'function') { + updateUserProfile({ foto_url: newUrl }) + } else { + const raw = localStorage.getItem('auth_user') + if (raw) { + const u = JSON.parse(raw) + u.profile = u.profile || {} + u.profile.foto_url = newUrl + u.profile.avatar_url = newUrl + localStorage.setItem('auth_user', JSON.stringify(u)) + } + } + } catch (err) { + console.warn('[PERFIL] erro ao persistir avatar no auth_user localStorage', err) + } + }} userName={editingData.full_name || userInfo.profile?.full_name || "Usuário"} /> diff --git a/susconecta/hooks/useAuth.tsx b/susconecta/hooks/useAuth.tsx index 1725b99..a2bec86 100644 --- a/susconecta/hooks/useAuth.tsx +++ b/susconecta/hooks/useAuth.tsx @@ -336,6 +336,26 @@ export function AuthProvider({ children }: { children: ReactNode }) { } }, [user?.userType, token, clearAuthData]) + // Allow updating the in-memory user profile and persist to localStorage. + const updateUserProfile = useCallback((partial: Partial) => { + try { + setUser((prev) => { + if (!prev) return prev + const next = { ...prev, profile: { ...(prev.profile || {}), ...(partial || {}) } } + try { + if (typeof window !== 'undefined') { + localStorage.setItem(AUTH_STORAGE_KEYS.USER, JSON.stringify(next)) + } + } catch (e) { + console.warn('[AUTH] Falha ao persistir user atualizado no localStorage:', e) + } + return next + }) + } catch (err) { + console.warn('[AUTH] updateUserProfile erro:', err) + } + }, []) + // Refresh token memoizado (usado pelo HTTP client) const refreshToken = useCallback(async (): Promise => { // Esta função é principalmente para compatibilidade @@ -350,8 +370,9 @@ export function AuthProvider({ children }: { children: ReactNode }) { token, login, logout, - refreshToken - }), [authStatus, user, token, login, logout, refreshToken]) + refreshToken, + updateUserProfile + }), [authStatus, user, token, login, logout, refreshToken, updateUserProfile]) // Inicialização única useEffect(() => { diff --git a/susconecta/types/auth.ts b/susconecta/types/auth.ts index 84a7adb..51ed66b 100644 --- a/susconecta/types/auth.ts +++ b/susconecta/types/auth.ts @@ -51,6 +51,8 @@ export interface AuthContextType { login: (email: string, password: string, userType: UserType) => Promise logout: () => Promise refreshToken: () => Promise + // Merge partial profile into the stored user (client-side convenience) + updateUserProfile?: (partial: Partial) => void } export interface AuthStorageKeys {