Skip to content

Commit a4501e2

Browse files
committed
Implement role-based redirection and add Providers component for session management
1 parent 3dfc7c1 commit a4501e2

5 files changed

Lines changed: 86 additions & 3 deletions

File tree

src/app/layout.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Metadata } from 'next';
22
import { Inter } from 'next/font/google';
33
import './globals.css';
4+
import { Providers } from './providers';
45

56
const inter = Inter({ subsets: ['latin'] });
67

@@ -16,7 +17,9 @@ export default function RootLayout({
1617
}) {
1718
return (
1819
<html lang="en">
19-
<body className={inter.className}>{children}</body>
20+
<body className={inter.className}>
21+
<Providers>{children}</Providers>
22+
</body>
2023
</html>
2124
);
2225
}

src/app/page.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,32 @@ import { BenefitsSection } from '@/components/landing/benefits-section';
55
import { SecuritySection } from '@/components/landing/security-section';
66
import { CTASection } from '@/components/landing/cta-section';
77
import { Footer } from '@/components/landing/footer';
8+
import { getServerSession } from 'next-auth/next';
9+
import { authOptions } from '@/lib/auth';
10+
import { redirect } from 'next/navigation';
11+
12+
export default async function Home() {
13+
// Server-side session check
14+
const session = await getServerSession(authOptions);
15+
16+
// If user is authenticated, redirect based on role
17+
if (session?.user) {
18+
const redirectMap: Record<string, string> = {
19+
admin: '/admin',
20+
doctor: '/doctor',
21+
nurse: '/nurse',
22+
pharmacist: '/pharmacist',
23+
'lab-tech': '/lab-tech',
24+
receptionist: '/receptionist',
25+
emergency: '/emergency',
26+
};
27+
28+
const role = session.user.role?.toLowerCase() || 'admin';
29+
const redirectPath = redirectMap[role] || '/admin';
30+
31+
redirect(redirectPath);
32+
}
833

9-
export default function Home() {
1034
return (
1135
<main className="overflow-hidden">
1236
<HeroSection />

src/app/providers.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use client';
2+
3+
import { SessionProvider } from 'next-auth/react';
4+
5+
export function Providers({ children }: { children: React.ReactNode }) {
6+
return <SessionProvider>{children}</SessionProvider>;
7+
}

src/components/auth-page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function AuthPage() {
3030
email,
3131
password,
3232
redirect: true,
33-
callbackUrl: "/admin",
33+
callbackUrl: "/",
3434
});
3535

3636
if (!result?.ok) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use client';
2+
3+
import { useEffect } from 'react';
4+
import { useSession } from 'next-auth/react';
5+
import { useRouter } from 'next/navigation';
6+
7+
/**
8+
* Wrapper component that redirects authenticated users to their respective dashboards.
9+
* This prevents users from seeing the landing page after logging in.
10+
*/
11+
export function LandingPageWrapper({ children }: { children: React.ReactNode }) {
12+
const { data: session, status } = useSession();
13+
const router = useRouter();
14+
15+
useEffect(() => {
16+
// Only redirect if we're certain the user is authenticated with valid data
17+
if (status === 'authenticated' && session?.user?.id && session.user.role) {
18+
// Redirect based on user role
19+
const redirectMap: Record<string, string> = {
20+
admin: '/admin',
21+
doctor: '/doctor',
22+
nurse: '/nurse',
23+
pharmacist: '/pharmacist',
24+
'lab-tech': '/lab-tech',
25+
receptionist: '/receptionist',
26+
emergency: '/emergency',
27+
};
28+
29+
const role = session.user.role.toLowerCase();
30+
const redirectPath = redirectMap[role] || '/admin';
31+
32+
// Use replace to prevent back button issues
33+
router.replace(redirectPath);
34+
}
35+
}, [status, session, router]);
36+
37+
// While session is loading, show nothing
38+
if (status === 'loading') {
39+
return null;
40+
}
41+
42+
// If authenticated, show nothing (effect will handle redirect)
43+
if (status === 'authenticated') {
44+
return null;
45+
}
46+
47+
// Only show landing page for unauthenticated users
48+
return <>{children}</>;
49+
}

0 commit comments

Comments
 (0)