feat: Redirect authenticated users from auth pages and enable event-driven profile data refresh in the settings sidebar.

This commit is contained in:
pradeepkumar
2026-02-09 00:03:45 +05:30
parent a2384fc7d9
commit eefc4bcd78
3 changed files with 109 additions and 54 deletions

View File

@@ -1,8 +1,64 @@
'use client';
import { useSession } from 'next-auth/react';
import { useRouter } from 'next/navigation';
import { useEffect } from 'react';
export default function AuthLayout({
children,
}: {
children: React.ReactNode;
}) {
const { data: session, status } = useSession();
const router = useRouter();
useEffect(() => {
if (status === 'loading') return;
// Redirect authenticated users away from auth pages
if (session) {
const userRole = (session.user as any)?.role;
if (userRole === 'AGENT') {
router.replace('/agent/dashboard');
} else {
router.replace('/user/dashboard');
}
}
}, [session, status, router]);
// Show loading spinner while checking authentication
if (status === 'loading') {
return (
<div className="min-h-screen flex items-center justify-center bg-gradient-to-b from-[#c4d9d4] to-[#f0f5fc]">
<div className="text-center">
<img
src="/assets/logo.svg"
alt="RE-Quest"
className="h-8 mx-auto mb-4"
/>
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-[#00293d] mx-auto"></div>
</div>
</div>
);
}
// Show loading while redirecting authenticated users
if (session) {
return (
<div className="min-h-screen flex items-center justify-center bg-gradient-to-b from-[#c4d9d4] to-[#f0f5fc]">
<div className="text-center">
<img
src="/assets/logo.svg"
alt="RE-Quest"
className="h-8 mx-auto mb-4"
/>
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-[#00293d] mx-auto"></div>
</div>
</div>
);
}
// Only render auth pages for unauthenticated users
return (
<div className="min-h-screen flex items-center justify-center bg-gradient-to-b from-[#c4d9d4] to-[#f0f5fc] py-12 px-4">
<div className="w-full max-w-[510px]">

View File

@@ -189,20 +189,13 @@ export function ProfileSettingsForm({
// Clean up local preview URL only after successful presigned URL
URL.revokeObjectURL(localPreviewUrl);
// Update the session to reflect the new avatar
await updateSession({
...session,
user: {
...session?.user,
image: presignedUrl,
},
});
// Dispatch event to notify header to refresh profile data
window.dispatchEvent(new Event('profile-updated'));
} catch {
// If presigned URL fails, keep the local preview (don't revoke it)
console.warn('Could not get presigned URL, using local preview');
// Still dispatch event to refresh header
window.dispatchEvent(new Event('profile-updated'));
}
setSuccessMessage('Profile picture updated successfully!');
@@ -254,15 +247,6 @@ export function ProfileSettingsForm({
}
setAvatarUrl(null);
// Update session
await updateSession({
...session,
user: {
...session?.user,
image: null,
},
});
// Dispatch event to notify header to refresh profile data
window.dispatchEvent(new Event('profile-updated'));

View File

@@ -1,6 +1,6 @@
'use client';
import { useState, useEffect } from 'react';
import { useState, useEffect, useCallback } from 'react';
import Image from 'next/image';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
@@ -31,11 +31,10 @@ export function SettingsSidebar({
avatarUrl: session?.user?.image || null,
});
useEffect(() => {
const fetchProfile = async () => {
const fetchProfile = useCallback(async () => {
try {
const role = (session?.user as any)?.role;
let avatarUrl = session?.user?.image || null;
let avatarUrl: string | null = null;
let name = session?.user?.name || 'User';
let title = 'User';
@@ -70,12 +69,28 @@ export function SettingsSidebar({
console.error('Failed to fetch profile for sidebar:', err);
// Keep session data as fallback
}
};
}, [session]);
// Fetch profile on mount
useEffect(() => {
if (session) {
fetchProfile();
}
}, [session]);
}, [session, fetchProfile]);
// Listen for profile update events to refresh the sidebar
useEffect(() => {
const handleProfileUpdate = () => {
if (session) {
fetchProfile();
}
};
window.addEventListener('profile-updated', handleProfileUpdate);
return () => {
window.removeEventListener('profile-updated', handleProfileUpdate);
};
}, [session, fetchProfile]);
const navItems: NavItem[] = [
{