-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.js
77 lines (49 loc) · 2.12 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
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
/* eslint-disable react-hooks/rules-of-hooks */
import { NextResponse } from 'next/server'
import jwtDecode from 'jwt-decode';
var _userName = 'Guest';
var _userEmail = `You're not logged in!`;
export function middleware(req) {
const userToken = req.cookies.get('gccxcsd_JWT');
const adminToken = req.cookies.get('gccxcsd_Admin');
let user = userToken ? JSON.parse(JSON.stringify(jwtDecode(userToken.value))) : undefined
// console.log(user);
_userName = user ? _userName.replace('Guest',user.userName) : 'Guest'
_userEmail = user ? _userEmail.replace(`You're not logged in!`,user.userEmail) : `You're not logged in!`
// console.log(_userName);
// console.log(_userEmail);
if(req.nextUrl.pathname.startsWith('/about')) {
// This logic is only applied to /about
console.log('about page');
}
// This logic is only applied to /admin/dashboard
if(!adminToken && req.nextUrl.pathname.startsWith('/admin/dashboard')) {
const url = req.nextUrl.clone()
url.pathname = '/admin_login'
return NextResponse.rewrite(url)
}
if(adminToken && (req.nextUrl.pathname.startsWith('/admin/dashboard'))) {
const url = req.nextUrl.clone()
url.pathname = '/admin/dashboard'
return NextResponse.rewrite(url)
}
// This logic is only applied to /alumni
// If the cookies are not found means user is not logged in.
// If user wants to access Alumni Page then we will redirect to Login Page
if(!userToken && req.nextUrl.pathname.startsWith('/alumni')) {
const url = req.nextUrl.clone()
url.pathname = '/login'
return NextResponse.rewrite(url)
}
// If the user is already logged in then that user can not access Login and Registration Page before Logout
// If user tries to access them manually by editing the url then they will be redirected to home page
if(userToken && (req.nextUrl.pathname.startsWith('/login') || req.nextUrl.pathname.startsWith('/register'))) {
const url = req.nextUrl.clone()
url.pathname = '/'
return NextResponse.rewrite(url)
}
}
export {
_userName,
_userEmail
}