-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
32 lines (26 loc) · 795 Bytes
/
proxy.ts
File metadata and controls
32 lines (26 loc) · 795 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 type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { isAdminSessionAuthorized } from '@/lib/auth';
const LOGIN_PATH = '/admin/login';
const LOGIN_API_PATH = '/admin/api/login';
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
if (
pathname === LOGIN_PATH ||
pathname === LOGIN_API_PATH ||
pathname.startsWith(`${LOGIN_API_PATH}/`)
) {
return NextResponse.next();
}
if (
pathname.startsWith('/admin') &&
!isAdminSessionAuthorized(request.cookies.get('admin_session')?.value)
) {
const loginUrl = new URL(LOGIN_PATH, request.url);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ['/admin/:path*'],
};