-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
23 lines (18 loc) · 690 Bytes
/
proxy.ts
File metadata and controls
23 lines (18 loc) · 690 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
const PROTECTED_PATHS = ['/profile', '/settings'];
export function proxy(request: NextRequest) {
const token = request.cookies.get('auth-token');
const { pathname } = request.nextUrl;
for (const path of PROTECTED_PATHS) {
if (pathname.startsWith(path) && !token) {
const loginUrl = new URL('/login', request.url);
loginUrl.searchParams.set('callbackUrl', pathname);
return NextResponse.redirect(loginUrl);
}
}
return NextResponse.next();
}
export const config = {
matcher: ['/profile/:path*', '/settings/:path*'],
};