Files
cronmaster/middleware.ts
2025-09-01 15:23:59 +01:00

38 lines
990 B
TypeScript

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
if (pathname.startsWith('/api/') || pathname.startsWith('/_next/') || pathname.includes('.')) {
return NextResponse.next()
}
const authPassword = process.env.AUTH_PASSWORD
if (!authPassword) {
return NextResponse.next()
}
const isAuthenticated = request.cookies.has('cronmaster-auth')
if (pathname === '/login') {
if (isAuthenticated || !authPassword) {
return NextResponse.redirect(new URL('/', request.url))
}
return NextResponse.next()
}
if (!isAuthenticated) {
return NextResponse.redirect(new URL('/login', request.url))
}
return NextResponse.next()
}
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|site.webmanifest|sw.js|app-icons).*)",
],
}