24 lines
692 B
TypeScript
24 lines
692 B
TypeScript
|
|
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'],
|
|
};
|