-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmiddleware.ts
99 lines (85 loc) · 2.86 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
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
import { NextRequest, NextResponse } from "next/server";
import { verifyToken } from "@/lib/jwt";
export default async function middleware(request: NextRequest) {
const tokenCookie = request.cookies.get("session");
const token = tokenCookie?.value;
if (!token) {
return NextResponse.redirect(new URL("/school-login", request.url));
}
try {
await verifyToken(token);
return NextResponse.next();
} catch (error) {
return NextResponse.redirect(new URL("/school-login", request.url));
}
}
export const config = {
matcher: ["/school-dashboard/:path*"],
};
// import { NextRequest, NextResponse } from "next/server";
// import { verifyToken } from "@/lib/jwt";
// // Define role constants
// const ROLES = {
// SCHOOL: "SCHOOL",
// STUDENT: "STUDENT",
// TEACHER: "TEACHER",
// };
// // Function to get the login page based on the role
// const getLoginPageForRole = (role: string): string => {
// switch (role) {
// case ROLES.SCHOOL:
// return "/school-login";
// case ROLES.STUDENT:
// return "/student-login";
// case ROLES.TEACHER:
// return "/teacher-login";
// default:
// return "/";
// }
// };
// // Function to get the required role for a given route
// const getRequiredRole = (pathname: string): string | null => {
// if (pathname.startsWith("/school")) {
// return ROLES.SCHOOL;
// } else if (pathname.startsWith("/student")) {
// return ROLES.STUDENT;
// } else if (pathname.startsWith("/teacher")) {
// return ROLES.TEACHER;
// }
// return null;
// };
// export default async function middleware(request: NextRequest) {
// console.log("Middleware triggered"); // Debugging log
// const tokenCookie = request.cookies.get("session");
// const token = tokenCookie?.value;
// const { pathname } = request.nextUrl;
// const requiredRole = getRequiredRole(pathname);
// // If the route requires a role and the role is null, skip validation
// if (!requiredRole) {
// return NextResponse.next();
// }
// if (!token) {
// const loginPage = getLoginPageForRole(requiredRole);
// return NextResponse.redirect(new URL(loginPage, request.url));
// }
// try {
// // Verify the token and extract the role
// const { role } = await verifyToken(token);
// if (role !== requiredRole) {
// const loginPage = getLoginPageForRole(requiredRole);
// return NextResponse.redirect(new URL(loginPage, request.url));
// }
// return NextResponse.next();
// } catch (error) {
// console.error("Token verification failed:", error); // Log the error
// const loginPage = getLoginPageForRole(requiredRole);
// return NextResponse.redirect(new URL(loginPage, request.url));
// }
// }
// export const config = {
// matcher: [
// "/school-dashboard/:path*",
// "/student-dashboard/:path*",
// "/teacher-dashboard/:path*",
// ],
// };