-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
46 lines (38 loc) · 1.31 KB
/
Copy pathproxy.ts
File metadata and controls
46 lines (38 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { auth } from '@/auth';
import { NextResponse } from 'next/server';
export const proxy = auth((req) => {
const isLoggedIn = !!req.auth;
const path = req.nextUrl.pathname;
const isLoginPage = path === '/login';
const isPublicPage =
path === '/' || path.startsWith('/verify') || path === '/site.webmanifest';
// Allow everyone (logged in or out) to access the public root page and verify pages
if (isPublicPage) {
return NextResponse.next();
}
// If not logged in and trying to access a protected page
if (!isLoggedIn && !isLoginPage) {
return NextResponse.redirect(new URL('/login', req.url));
}
// If logged in and trying to access the login page
if (isLoggedIn && isLoginPage) {
return NextResponse.redirect(new URL('/dashboard', req.url));
}
// Role-based protection
if (isLoggedIn) {
const role = req.auth?.user?.role;
const isRestrictedRoute =
path.startsWith('/users') ||
path.startsWith('/settings') ||
path.startsWith('/audit');
if (role === 'Volunteer' && isRestrictedRoute) {
return NextResponse.redirect(new URL('/dashboard', req.url));
}
}
return NextResponse.next();
});
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp|webmanifest|json)$).*)',
],
};