Files
no-copy-frontend/src/proxy.ts
T

29 lines
893 B
TypeScript
Raw Normal View History

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'
2025-12-09 16:35:47 +07:00
import { decrypt } from '@/app/actions/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;
const session = await decrypt(cookie);
2025-11-28 15:28:15 +07:00
if (isProtectedRoute && !session?.token) {
2025-12-02 17:15:09 +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
2025-12-02 17:15:09 +07:00
if (path === '/') {
return NextResponse.redirect(new URL('/pages/dashboard', request.nextUrl));
}
return NextResponse.next();
2025-11-25 17:13:16 +07:00
}
export const config = {
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)']
};