first commit
This commit is contained in:
32
middleware.ts
Normal file
32
middleware.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
export function middleware(request: NextRequest) {
|
||||
const { pathname } = request.nextUrl;
|
||||
const adminSession = request.cookies.get('admin_session');
|
||||
|
||||
// Allow requests to /login and static assets
|
||||
if (
|
||||
pathname.startsWith('/login') ||
|
||||
pathname.startsWith('/_next') ||
|
||||
pathname.startsWith('/api') ||
|
||||
pathname === '/favicon.ico'
|
||||
) {
|
||||
// If already logged in and trying to access login page, redirect to home
|
||||
if (pathname.startsWith('/login') && adminSession) {
|
||||
return NextResponse.redirect(new URL('/', request.url));
|
||||
}
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// Redirect to /login if no session exists
|
||||
if (!adminSession) {
|
||||
return NextResponse.redirect(new URL('/login', request.url));
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
|
||||
};
|
||||
Reference in New Issue
Block a user