-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
27 lines (21 loc) · 774 Bytes
/
Copy pathproxy.ts
File metadata and controls
27 lines (21 loc) · 774 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
import { NextRequest, NextResponse } from 'next/server';
import { decrypt, SESSION_COOKIE_NAME } from '@/lib/admin/session';
export async function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const token = request.cookies.get(SESSION_COOKIE_NAME)?.value;
const isAuthenticated = token ? (await decrypt(token))?.isAdmin === true : false;
const isLoginPage = pathname === '/admin/login';
if (isLoginPage) {
if (isAuthenticated) {
return NextResponse.redirect(new URL('/admin', request.url));
}
return NextResponse.next();
}
if (!isAuthenticated) {
return NextResponse.redirect(new URL('/admin/login', request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ['/admin/:path*'],
};