-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
52 lines (43 loc) · 1.61 KB
/
middleware.js
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
export { default } from "next-auth/middleware";
// export const config = {
// matcher: ["/registrarTorneo", "/admin/registrados"],
// };
import { NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
export async function middleware(request) {
const token = await getToken({
req: request,
secret: process.env.NEXTAUTH_SECRET,
});
if (!token) return NextResponse.redirect(new URL("/auth/login", request.url));
// Check the role and redirect based on the role
switch (token.role) {
case "admin":
if (!request.nextUrl.pathname.startsWith("/admin")) {
return NextResponse.redirect(new URL("/admin/registrados", request.url));
}
break;
case "participante":
// Add the paths that the nurse can access here
if (!request.nextUrl.pathname.startsWith("/registrarTorneo")) {
return NextResponse.redirect(new URL("/registrarTorneo", request.url));
}
break;
default:
return NextResponse.redirect(new URL("/auth/login", request.url));
}
}
export const config = {
matcher: [
// Match all routes except the ones that start with /login and api and the static folder
"/((?!api|_next/static|_next/image|favicon.ico|auth/login|auth/register|auth/change-password|auth/forget-password).*)",
],
};
// if (
// !request.nextUrl.pathname.startsWith("/admin/actividad") &&
// !request.nextUrl.pathname.startsWith("/admin/programacion") &&
// !request.nextUrl.pathname.startsWith("/admin/registrados")
// ) {
// return NextResponse.redirect(new URL("/admin/registrados", request.url));
// }
// break;