chore: migrate middleware to proxy as per Next.js 2026 convention
This commit is contained in:
27
proxy.ts
Normal file
27
proxy.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { decrypt } from '@/app/lib/auth';
|
||||
|
||||
export async function proxy(request: NextRequest) {
|
||||
const session = request.cookies.get('session')?.value;
|
||||
const { pathname } = request.nextUrl;
|
||||
|
||||
// Protect admin routes
|
||||
if (pathname.startsWith('/admin')) {
|
||||
if (!session) {
|
||||
return NextResponse.redirect(new URL('/login', request.url));
|
||||
}
|
||||
|
||||
try {
|
||||
await decrypt(session);
|
||||
return NextResponse.next();
|
||||
} catch (e) {
|
||||
return NextResponse.redirect(new URL('/login', request.url));
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ['/admin/:path*'],
|
||||
};
|
||||
Reference in New Issue
Block a user