Add src/middleware.ts
This commit is contained in:
26
src/middleware.ts
Normal file
26
src/middleware.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
export function middleware(request: NextRequest) {
|
||||
const { pathname } = request.nextUrl;
|
||||
|
||||
// Protect /admin routes
|
||||
if (pathname.startsWith('/admin') && !pathname.startsWith('/admin/login')) {
|
||||
const adminToken = request.cookies.get('adminToken');
|
||||
|
||||
if (adminToken?.value === 'mock-admin-token') {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
const url = request.nextUrl.clone();
|
||||
url.pathname = '/admin/login';
|
||||
url.searchParams.set('redirected', 'true'); // Optional: Add a query param to indicate redirection
|
||||
return NextResponse.redirect(url);
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ['/admin/:path*'], // Apply middleware to all routes under /admin
|
||||
};
|
||||
Reference in New Issue
Block a user