31 lines
939 B
TypeScript
31 lines
939 B
TypeScript
import { NextResponse, NextRequest } from 'next/server';
|
|
import { cookies } from 'next/headers'
|
|
import { decrypt } from '@/app/lib/session';
|
|
|
|
const protectedRoutes = ['/pages']
|
|
const publicRoutes = ['/login', '/register', '/']
|
|
|
|
export default async function proxy(request: NextRequest) {
|
|
const path = request.nextUrl.pathname;
|
|
const isProtectedRoute = protectedRoutes.includes(path.slice(0, 6));
|
|
const isPublicRoute = publicRoutes.includes(path);
|
|
|
|
const cookie = (await cookies()).get('session')?.value;
|
|
console.log("-cookie-");
|
|
console.log(!!cookie);
|
|
const session = await decrypt(cookie);
|
|
|
|
if (isProtectedRoute && !session?.token) {
|
|
return NextResponse.redirect(new URL('/login', request.nextUrl));
|
|
}
|
|
|
|
if (path === '/') {
|
|
return NextResponse.redirect(new URL('/pages/dashboard', request.nextUrl));
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)']
|
|
}; |