2025-11-25 17:13:16 +07:00
|
|
|
import { NextResponse, NextRequest } from 'next/server';
|
2025-11-27 21:33:53 +07:00
|
|
|
import { cookies } from 'next/headers'
|
|
|
|
|
import { decrypt } from '@/app/lib/session';
|
2025-11-25 17:13:16 +07:00
|
|
|
|
2025-11-27 21:33:53 +07:00
|
|
|
const protectedRoutes = ['/pages']
|
|
|
|
|
const publicRoutes = ['/login', '/register', '/']
|
2025-11-25 17:13:16 +07:00
|
|
|
|
2025-11-27 21:33:53 +07:00
|
|
|
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);
|
|
|
|
|
|
2025-11-28 15:28:15 +07:00
|
|
|
if (isProtectedRoute && !session?.token) {
|
2025-11-27 21:33:53 +07:00
|
|
|
return NextResponse.redirect(new URL('/login', request.nextUrl))
|
2025-11-25 17:13:16 +07:00
|
|
|
}
|
2025-11-27 21:33:53 +07:00
|
|
|
|
|
|
|
|
return NextResponse.next();
|
2025-11-25 17:13:16 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const config = {
|
|
|
|
|
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)']
|
|
|
|
|
};
|