-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmiddleware.ts
More file actions
189 lines (168 loc) · 5.87 KB
/
middleware.ts
File metadata and controls
189 lines (168 loc) · 5.87 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { verifyToken, verifyMentorToken, verifyAmbassadorToken } from '@/lib/jwt';
const discoveryLinkHeader = [
'</.well-known/api-catalog>; rel="api-catalog"',
'</openapi.json>; rel="service-desc"; type="application/openapi+json"',
'</api-docs>; rel="service-doc"; type="text/html"',
'</.well-known/agent-skills/index.json>; rel="describedby"; type="application/json"',
'</llms.txt>; rel="alternate"; type="text/plain"',
].join(', ');
function withDiscoveryHeaders(response: NextResponse): NextResponse {
response.headers.set('Link', discoveryLinkHeader);
return response;
}
export async function middleware(request: NextRequest) {
const path = request.nextUrl.pathname;
const acceptHeader = request.headers.get('accept')?.toLowerCase() ?? '';
const wantsMarkdown = acceptHeader.includes('text/markdown');
const isStaticFileRequest = /\.[a-z0-9]+$/i.test(path);
const excludedMarkdownPaths = new Set([
'/agent-markdown',
'/robots.txt',
'/sitemap.xml',
'/llms.txt',
'/llms-full.txt',
]);
const isPrivateOrApiPath =
path.startsWith('/api') ||
path.startsWith('/admin') ||
path.startsWith('/mentor') ||
path.startsWith('/ambassador') ||
path.startsWith('/.well-known');
if (
wantsMarkdown &&
!isStaticFileRequest &&
!excludedMarkdownPaths.has(path) &&
!isPrivateOrApiPath
) {
const markdownUrl = new URL('/agent-markdown', request.url);
markdownUrl.searchParams.set('source', path);
return NextResponse.rewrite(markdownUrl);
}
// Redirect root to our-story page
if (path === '/') {
return withDiscoveryHeaders(
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();
}
if (path === '/our-story') {
return withDiscoveryHeaders(NextResponse.next());
}
return NextResponse.next();
}
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico|favicons|images|.*\\..*).*)',
]
};