-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
32 lines (24 loc) · 857 Bytes
/
middleware.ts
File metadata and controls
32 lines (24 loc) · 857 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
26
27
28
29
30
31
32
import { NextRequest, NextResponse } from 'next/server';
import { ACCESS_TOKEN } from '@/constants/storage';
import { UrlType } from '@/constants/url';
import { isOnboardingUrl, isWorkspaceUrl } from '@/utils/url';
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const isProtectedUrl = isWorkspaceUrl(pathname as UrlType) || isOnboardingUrl(pathname as UrlType);
const cookie = request.cookies.get(ACCESS_TOKEN);
const token = cookie?.value;
if (!token) {
// TODO: token validation
if (isProtectedUrl) {
const absoluteURL = new URL('/login', request.nextUrl.origin);
return NextResponse.redirect(absoluteURL.toString());
}
}
const headers = new Headers(request.headers);
headers.set('x-pathname', pathname);
return NextResponse.next({
request: {
headers: headers,
},
});
}