36 lines
1.2 KiB
Text
36 lines
1.2 KiB
Text
|
|
/**
|
||
|
|
* TEE OFF SECURITY MIDDLEWARE v1.1
|
||
|
|
* ---------------------------------------------------------------------------
|
||
|
|
* REGEL: Beskytter alle ruter under /admin (unntatt /admin/login).
|
||
|
|
* FUNKSJON: Sjekker for admin_session cookie og omdirigerer hvis den mangler.
|
||
|
|
* RETTING: Flyttet NextRequest til next/server for å fikse build-error.
|
||
|
|
* ---------------------------------------------------------------------------
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { NextResponse, type NextRequest } from 'next/server';
|
||
|
|
|
||
|
|
export function middleware(request: NextRequest) {
|
||
|
|
const { pathname } = request.nextUrl;
|
||
|
|
const session = request.cookies.get('admin_session');
|
||
|
|
|
||
|
|
// 1. Tillat alltid tilgang til innloggingssiden
|
||
|
|
if (pathname.startsWith('/admin/login')) {
|
||
|
|
return NextResponse.next();
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. Beskytt alle andre ruter under /admin
|
||
|
|
if (pathname.startsWith('/admin')) {
|
||
|
|
if (!session) {
|
||
|
|
// Ingen sesjon funnet -> Send til innlogging
|
||
|
|
const loginUrl = new URL('/admin/login', request.url);
|
||
|
|
return NextResponse.redirect(loginUrl);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return NextResponse.next();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Definer hvilke ruter middleware skal kjøre på
|
||
|
|
export const config = {
|
||
|
|
matcher: ['/admin/:path*'],
|
||
|
|
};
|