-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.ts
58 lines (47 loc) · 1.59 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
import {withAuth, NextRequestWithAuth} from "next-auth/middleware";
import {NextResponse} from "next/server";
export default withAuth(
function middleware(request: NextRequestWithAuth) {
const user =
request.nextauth.token?.role === "user" &&
(request.nextUrl.pathname.startsWith("/exercise") ||
request.nextUrl.pathname.startsWith("/diet") ||
request.nextUrl.pathname.startsWith("/fees") ||
request.nextUrl.pathname.startsWith("/add-user") ||
request.nextUrl.pathname.startsWith("/students"));
if (user) {
return NextResponse.rewrite(new URL("/unauthorized", request.url));
}
const adminOrTrainer =
request.nextUrl.pathname.startsWith("/user") &&
request.nextauth.token?.role !== "user";
if (adminOrTrainer) {
return NextResponse.rewrite(new URL("/unauthorized", request.url));
}
const admin = request.nextauth.token?.role !== "admin" && request.nextUrl.pathname.startsWith("/manage-user");
if (admin) {
return NextResponse.rewrite(new URL("/unauthorized", request.url));
}
},
{
callbacks: {
authorized: ({token}) => !!token,
},
}
);
export const config = {
matcher: [
"/",
"/api",
"/add-user",
"/manage-user",
"/profile",
"/notifications",
"/students/:path*",
"/trainers/:path*",
"/fees",
"/exercise/:path*",
"/diet/:path*",
"/user/:path*",
],
};