feat: Implement authentication checks for profile actions and user layout, and add presigned URL fetching for file viewing.
This commit is contained in:
@@ -6,6 +6,18 @@ import { useEffect } from 'react';
|
||||
import { Footer } from '@/components/layout/Footer';
|
||||
import { CommonHeader } from '@/components/layout/CommonHeader';
|
||||
|
||||
// Pages that don't require authentication
|
||||
const publicPaths = [
|
||||
'/user/dashboard',
|
||||
'/user/profiles',
|
||||
'/user/profile/', // Agent profile view (includes /user/profile/[id])
|
||||
];
|
||||
|
||||
// Check if current path is public
|
||||
const isPublicPath = (pathname: string) => {
|
||||
return publicPaths.some(path => pathname === path || pathname.startsWith(path));
|
||||
};
|
||||
|
||||
export default function UserLayout({
|
||||
children,
|
||||
}: {
|
||||
@@ -16,23 +28,47 @@ export default function UserLayout({
|
||||
const pathname = usePathname();
|
||||
|
||||
const isDashboard = pathname === '/user/dashboard';
|
||||
const isPublic = isPublicPath(pathname);
|
||||
|
||||
useEffect(() => {
|
||||
if (status === 'loading') return;
|
||||
|
||||
// Allow public pages without authentication
|
||||
if (isPublic) {
|
||||
// If logged in as agent, redirect to agent dashboard only from user dashboard
|
||||
if (session && pathname === '/user/dashboard') {
|
||||
const userRole = (session.user as any)?.role;
|
||||
if (userRole === 'AGENT') {
|
||||
router.replace('/agent/dashboard');
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Protected pages require login
|
||||
if (!session) {
|
||||
router.replace('/login');
|
||||
return;
|
||||
}
|
||||
|
||||
// Redirect agents to agent dashboard
|
||||
// Redirect agents to agent dashboard for protected user pages
|
||||
const userRole = (session.user as any)?.role;
|
||||
if (userRole === 'AGENT') {
|
||||
router.replace('/agent/dashboard');
|
||||
}
|
||||
}, [session, status, router]);
|
||||
}, [session, status, router, pathname, isPublic]);
|
||||
|
||||
if (status === 'loading' || !session) {
|
||||
// Show loading only for protected pages while checking auth
|
||||
if (status === 'loading' && !isPublic) {
|
||||
return (
|
||||
<div className="min-h-screen bg-white flex items-center justify-center">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-[#00293d]"></div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Redirect protected pages if not logged in
|
||||
if (!session && !isPublic) {
|
||||
return (
|
||||
<div className="min-h-screen bg-white flex items-center justify-center">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-[#00293d]"></div>
|
||||
|
||||
Reference in New Issue
Block a user