create a comprehensive admin dashbioard with permalink /admin/ and creat
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
|
||||
import {NextResponse} from 'next/server';
|
||||
import type {NextRequest} from 'next/server';
|
||||
|
||||
export function middleware(request: NextRequest) {
|
||||
const session = request.cookies.get('session');
|
||||
|
||||
// Redirect to login if trying to access /admin without a session
|
||||
if (request.nextUrl.pathname.startsWith('/admin') && !session) {
|
||||
return NextResponse.redirect(new URL('/login', request.url));
|
||||
}
|
||||
|
||||
// Redirect to admin if trying to access /login with a session
|
||||
if (request.nextUrl.pathname === '/login' && session) {
|
||||
return NextResponse.redirect(new URL('/admin', request.url));
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ['/admin/:path*', '/login'],
|
||||
};
|
||||
Reference in New Issue
Block a user