-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
103 lines (94 loc) · 3.04 KB
/
middleware.ts
File metadata and controls
103 lines (94 loc) · 3.04 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const token = request.cookies.get("token")?.value;
const { pathname } = request.nextUrl;
// Protected routes
const protectedRoutes = [
"/dashboard",
"/admin/dashboard",
"/admin/scanner",
"/super-admin/dashboard",
];
const authRoutes = ["/login", "/register"];
// Redirect authenticated users away from auth pages
if (token && authRoutes.some((route) => pathname === route)) {
// Decode token to check role (basic decode for redirect only, full verification in API routes)
try {
const payload = JSON.parse(
Buffer.from(token.split(".")[1], "base64").toString(),
);
// Basic expiry check
if (payload.exp && payload.exp * 1000 < Date.now()) {
// Token expired, clear cookie and continue
const response = NextResponse.next();
response.cookies.delete("token");
return response;
}
if (payload.role === "user") {
return NextResponse.redirect(new URL("/dashboard", request.url));
} else if (payload.role === "admin") {
return NextResponse.redirect(new URL("/admin/dashboard", request.url));
} else if (payload.role === "superadmin") {
return NextResponse.redirect(
new URL("/super-admin/dashboard", request.url),
);
}
} catch (error) {
// Invalid token, clear cookie
const response = NextResponse.next();
response.cookies.delete("token");
return response;
}
}
// Redirect authenticated users away from login pages (exact match)
if (token) {
if (pathname === "/admin") {
try {
const payload = JSON.parse(
Buffer.from(token.split(".")[1], "base64").toString(),
);
if (payload.role === "admin") {
return NextResponse.redirect(
new URL("/admin/dashboard", request.url),
);
}
} catch (error) {
// Invalid token
}
} else if (pathname === "/super-admin") {
try {
const payload = JSON.parse(
Buffer.from(token.split(".")[1], "base64").toString(),
);
if (payload.role === "superadmin") {
return NextResponse.redirect(
new URL("/super-admin/dashboard", request.url),
);
}
} catch (error) {
// Invalid token
}
}
}
// Protect routes from unauthenticated users
if (!token && protectedRoutes.some((route) => pathname.startsWith(route))) {
if (pathname.startsWith("/admin")) {
return NextResponse.redirect(new URL("/admin", request.url));
} else if (pathname.startsWith("/super-admin")) {
return NextResponse.redirect(new URL("/super-admin", request.url));
} else {
return NextResponse.redirect(new URL("/login", request.url));
}
}
return NextResponse.next();
}
export const config = {
matcher: [
"/dashboard/:path*",
"/admin/:path*",
"/super-admin/:path*",
"/login",
"/register",
],
};