-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
37 lines (28 loc) · 949 Bytes
/
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
import { NextRequest, NextResponse } from 'next/server';
export function middleware(request: NextRequest) {
const token = request.cookies.get('zoommentToken')?.value;
const url = request.nextUrl;
const newToken = url.searchParams.get('zoommentToken');
//TODO add token validation
if (newToken) {
url.searchParams.delete('zoommentToken');
const res = NextResponse.redirect(url);
res.cookies.set('zoommentToken', newToken, {
expires: new Date(+new Date() + 30000000000),
path: '/',
domain: `.${url.hostname}`,
sameSite: 'none',
secure: true,
});
return res;
}
if (token && url.pathname.startsWith('/auth')) {
return Response.redirect(new URL('/dashboard', url));
}
if (!token && url.pathname.startsWith('/dashboard')) {
return Response.redirect(new URL('/auth', url));
}
}
export const config = {
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'],
};