-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmiddleware.ts
More file actions
148 lines (132 loc) · 4.51 KB
/
middleware.ts
File metadata and controls
148 lines (132 loc) · 4.51 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { verifyToken, verifyMentorToken, verifyAmbassadorToken } from '@/lib/jwt';
export async function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;
// Redirect root to our-story page
if (path === '/') {
return NextResponse.redirect(new URL('/our-story', request.url));
}
const token = request.cookies.get('admin-token')?.value;
const adminCodeVerified = request.cookies.get('admin-code-verified')?.value;
const mentorToken = request.cookies.get('mentor-token')?.value;
const mentorAccessVerified = request.cookies.get('mentor-access-verified')?.value;
const ambassadorToken = request.cookies.get('ambassador-token')?.value;
const ambassadorAccessVerified = request.cookies.get('ambassador-access-verified')?.value;
// Allow access to admin code verification page
if (path === '/admin') {
if (token) {
const isValid = await verifyToken(token);
if (isValid) {
return NextResponse.redirect(new URL('/admin/dashboard', request.url));
}
}
return NextResponse.next();
}
// Protect login page - only accessible after code verification
if (path === '/admin/login') {
if (!adminCodeVerified) {
return NextResponse.redirect(new URL('/admin', request.url));
}
if (token) {
const isValid = await verifyToken(token);
if (isValid) {
return NextResponse.redirect(new URL('/admin/dashboard', request.url));
}
}
return NextResponse.next();
}
// Protect all other admin routes
if (path.startsWith('/admin')) {
if (!token) {
return NextResponse.redirect(new URL('/admin', request.url));
}
const isValid = await verifyToken(token);
if (!isValid) {
return NextResponse.redirect(new URL('/admin', request.url));
}
return NextResponse.next();
}
// Mentor authentication routes
// Allow access to mentor access code page
if (path === '/mentor') {
if (mentorToken) {
const isValid = await verifyMentorToken(mentorToken);
if (isValid) {
return NextResponse.redirect(new URL('/mentor/dashboard', request.url));
}
}
return NextResponse.next();
}
// Mentor login page - only accessible after access code verification
if (path === '/mentor/login') {
if (!mentorAccessVerified) {
return NextResponse.redirect(new URL('/mentor', request.url));
}
if (mentorToken) {
const isValid = await verifyMentorToken(mentorToken);
if (isValid) {
return NextResponse.redirect(new URL('/mentor/dashboard', request.url));
}
}
return NextResponse.next();
}
// Protect all other mentor routes
if (path.startsWith('/mentor/') && path !== '/mentor/login') {
if (!mentorToken) {
return NextResponse.redirect(new URL('/mentor', request.url));
}
const isValid = await verifyMentorToken(mentorToken);
if (!isValid) {
return NextResponse.redirect(new URL('/mentor', request.url));
}
return NextResponse.next();
}
// Ambassador authentication routes
// Allow access to ambassador access code page
if (path === '/ambassador') {
if (ambassadorToken) {
const isValid = await verifyAmbassadorToken(ambassadorToken);
if (isValid) {
return NextResponse.redirect(new URL('/ambassador/dashboard', request.url));
}
}
return NextResponse.next();
}
// Ambassador login page - only accessible after access code verification
if (path === '/ambassador/login') {
if (!ambassadorAccessVerified) {
return NextResponse.redirect(new URL('/ambassador', request.url));
}
if (ambassadorToken) {
const isValid = await verifyAmbassadorToken(ambassadorToken);
if (isValid) {
return NextResponse.redirect(new URL('/ambassador/dashboard', request.url));
}
}
return NextResponse.next();
}
// Protect all other ambassador routes
if (path.startsWith('/ambassador/') && path !== '/ambassador/login') {
if (!ambassadorToken) {
return NextResponse.redirect(new URL('/ambassador', request.url));
}
const isValid = await verifyAmbassadorToken(ambassadorToken);
if (!isValid) {
return NextResponse.redirect(new URL('/ambassador', request.url));
}
return NextResponse.next();
}
return NextResponse.next();
}
export const config = {
matcher: [
'/',
'/admin/:path*',
'/api/admin/:path*',
'/mentor/:path*',
'/api/mentor/:path*',
'/ambassador/:path*',
'/api/ambassador/:path*'
]
};