-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.ts
63 lines (57 loc) · 1.75 KB
/
middleware.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { NextResponse, NextRequest } from "next/server";
import { getToken } from "next-auth/jwt";
// Route access rules
// Role: SUPERADMIN > ADMIN > null (default for all authenticated users)
const ROUTE_RULES: Record<string, { roles: string[] | null }> = {
"/admin": { roles: ["ADMIN", "SUPERADMIN"] },
"/admin/manage-user": { roles: ["SUPERADMIN"] },
"/quiz": { roles: null },
};
// Middleware function
export async function middleware(request: NextRequest) {
const token = (await getToken({
req: request,
secret: process.env.SECRET,
})) as { role?: string } | null;
const { pathname } = new URL(request.url);
// Match the route with access rules
for (const [route, { roles }] of Object.entries(ROUTE_RULES)) {
if (pathname.startsWith(route)) {
if (!token) {
return redirectToLogin(request);
}
if (roles && !roles.includes(token.role || "")) {
return redirectToSafeUrl(request);
}
return NextResponse.next();
}
}
return NextResponse.next();
}
/**
* Redirects the user to the login page.
* @param request - The incoming request.
*/
function redirectToLogin(request: NextRequest): NextResponse {
return NextResponse.redirect(new URL("/auth/login", request.url));
}
/**
* Redirects the user to a safe fallback URL based on referer or root path.
* @param request - The incoming request.
*/
function redirectToSafeUrl(request: NextRequest): NextResponse {
const referer = request.headers.get("referer");
if (referer) {
try {
const refererUrl = new URL(referer);
if (refererUrl.origin === new URL(request.url).origin) {
return NextResponse.redirect(
new URL(refererUrl.pathname, request.url)
);
}
} catch {
// Invalid referer URL; fallback
}
}
return NextResponse.redirect(new URL("/", request.url));
}