25 lines
833 B
JavaScript
25 lines
833 B
JavaScript
import { NextResponse } from 'next/server';
|
|
|
|
export function middleware(request) {
|
|
// Prüfe nur Admin-Routen
|
|
if (request.nextUrl.pathname.startsWith('/admin')) {
|
|
const isAuthenticated = request.cookies.get('isAuthenticated');
|
|
|
|
// Wenn nicht authentifiziert und nicht bereits auf der Login-Seite, leite zur Login-Seite weiter
|
|
if (!isAuthenticated && !request.nextUrl.pathname.startsWith('/admin/login')) {
|
|
return NextResponse.redirect(new URL('/admin/login', request.url));
|
|
}
|
|
|
|
// Wenn authentifiziert und auf der Login-Seite, leite zum Admin-Panel weiter
|
|
if (isAuthenticated && request.nextUrl.pathname.startsWith('/admin/login')) {
|
|
return NextResponse.redirect(new URL('/admin', request.url));
|
|
}
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: '/admin/:path*'
|
|
};
|