-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
28 lines (21 loc) · 839 Bytes
/
Copy pathproxy.js
File metadata and controls
28 lines (21 loc) · 839 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
import { NextResponse } from 'next/server';
import { decryptSession } from '@/lib/auth';
const protectedRoutes = ['/dashboard'];
const publicRoutes = ['/login', '/register'];
export async function proxy(req) {
const path = req.nextUrl.pathname;
const isProtectedRoute = protectedRoutes.includes(path);
const isPublicRoute = publicRoutes.includes(path);
const cookie = req.cookies.get('session')?.value;
const session = cookie ? await decryptSession(cookie) : null;
if (isProtectedRoute && !session?.userId) {
return NextResponse.redirect(new URL('/login', req.nextUrl));
}
if (isPublicRoute && session?.userId) {
return NextResponse.redirect(new URL('/dashboard', req.nextUrl));
}
return NextResponse.next();
}
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};