-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
105 lines (91 loc) · 3.3 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
100
101
102
103
104
105
import { NextResponse,NextRequest} from 'next/server'
import { getCookie } from 'cookies-next';
import { cookies } from 'next/headers';
export function middleware (request: NextRequest) {
const token = getCookie("token", {cookies}) || null
const role = getCookie("role", {cookies}) || null
const expiration = getCookie("expiration", {cookies})|| ""
var date = expiration;
var datearray = date.split("/");
var newdate = datearray[2] + '-' + datearray[1] + '-' + datearray[0];
const dateExpiration = Date.parse(newdate);
const dateNow = Date.now();
// const protectedRoutes = ['parent', 'admin', 'teacher'];
// var path: string = request.nextUrl.pathname.toLowerCase();
// if (protectedRoutes.some(x => path.startsWith('/' + x.toLowerCase()))) {
// }
//protect routes
//redirect routes
if (request.nextUrl.pathname.includes("/parent")) {
if(token && role === "Parent"){
if(request.nextUrl.pathname !== "/parent"){
return NextResponse.rewrite(new URL(request.nextUrl.pathname, request.url))
}
}
else {
return NextResponse.rewrite(new URL('/login', request.url))
}
}
if ((request.nextUrl.pathname).includes('/admin')) {
if(token && role === "Admin") {
if(request.nextUrl.pathname !== "/admin"){
return NextResponse.rewrite(new URL(request.nextUrl.pathname, request.url))
}
}else {
return NextResponse.rewrite(new URL('/login', request.url))
}
}
if ((request.nextUrl.pathname).includes('/teacher')) {
if(token && role === "Teacher") {
if(request.nextUrl.pathname !== "/teacher"){
return NextResponse.rewrite(new URL(request.nextUrl.pathname, request.url))
}
}else {
return NextResponse.rewrite(new URL('/login', request.url))
}
}
if (request.nextUrl.pathname.includes('/login')) {
if(token && role === "Parent"){
return NextResponse.redirect(new URL('/parent', request.url))
}else if(token && role === "Admin") {
return NextResponse.redirect(new URL('/admin', request.url))
}else if(token && role === "Teacher") {
return NextResponse.redirect(new URL('/teacher', request.url))
}else {
return NextResponse.rewrite(new URL('/login', request.url))
}
}
if ((request.nextUrl.pathname).includes('/profile')) {
if(token && role === "Parent"){
return NextResponse.redirect(new URL('/parent/profile', request.url))
}else if(token && role === "Admin") {
return NextResponse.redirect(new URL('/admin/profile', request.url))
}else if(token && role === "Teacher") {
return NextResponse.redirect(new URL('/teacher/profile', request.url))
}else {
return NextResponse.rewrite(new URL('/login', request.url))
}
}
if ((request.nextUrl.pathname).includes('/choose-user')) {
if(token == null){
return NextResponse.rewrite(new URL('/login', request.url))
}
}
if (
dateNow > dateExpiration
) {
request.cookies.delete("token");
request.cookies.delete("role");
request.cookies.delete("expiration");
const response = NextResponse.redirect(new URL("/login", request.url));
response.cookies.delete("token");
response.cookies.delete("role");
response.cookies.delete("expiration");
return response;
}
}
export const config = {
matcher: [
'/((?!api|_next|.*\\..*).*)'
]
}