Repository navigation
Expand file tree
/
Copy pathmiddleware.ts
More file actions
69 lines (57 loc) · 1.95 KB
/
Copy pathmiddleware.ts
File metadata and controls
69 lines (57 loc) · 1.95 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
import { NextRequest, NextResponse } from "next/server";
import { verifyToken } from "./lib/auth";
import { ROUTES_CONSTANT } from "./lib/routes.constant";
import { UserRole } from "./types";
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Handle login page redirect for authenticated users
if (pathname === ROUTES_CONSTANT.LOGIN) {
const token = request.cookies.get("admin-token")?.value;
if (token) {
// Verify the tokens
const user = await verifyToken(token);
if (user && user.role === UserRole.ADMIN) {
// User is already logged in, redirect to dashboard
return NextResponse.redirect(
new URL(ROUTES_CONSTANT.DASHBOARD, request.url)
);
}
}
// No valid token, allow access to login page
return NextResponse.next();
}
// Protect other admin routes
if (pathname.startsWith(ROUTES_CONSTANT.DASHBOARD)) {
const token = request.cookies.get("admin-token")?.value;
if (!token) {
// No token found, redirect to login
return NextResponse.redirect(new URL(ROUTES_CONSTANT.LOGIN, request.url));
}
// Verify the token
const user = await verifyToken(token);
if (!user || user.role !== UserRole.ADMIN) {
// Invalid token, redirect to login
const response = NextResponse.redirect(
new URL(ROUTES_CONSTANT.LOGIN, request.url)
);
response.cookies.delete("admin-token");
return response;
}
// Token is valid, allow access to admin routes
return NextResponse.next();
}
// For non-admin routes, just continue
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!api|_next/static|_next/image|favicon.ico).*)",
],
};