-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
25 lines (23 loc) · 823 Bytes
/
middleware.ts
File metadata and controls
25 lines (23 loc) · 823 Bytes
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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { AUTH_ROUTES, GUEST_ROUTES } from './config';
export function middleware(request: NextRequest) {
let response = NextResponse.next();
const cookies = request.cookies.getAll();
const userHasToken = cookies.some((cookie) => cookie.name === 'XSRF-TOKEN');
const pathname = request.nextUrl.pathname;
for (const regex of AUTH_ROUTES) {
if (regex.test(pathname) && !userHasToken) {
response = NextResponse.rewrite(new URL('/', request.url));
}
}
for (const regex of GUEST_ROUTES) {
if (regex.test(pathname) && userHasToken) {
response = NextResponse.rewrite(new URL('/', request.url));
}
}
return response;
}
export const config = {
matcher: ['/add-character', '/login', '/'],
};