30 lines
834 B
TypeScript
30 lines
834 B
TypeScript
import { NextResponse } from 'next/server'
|
|
import type { NextRequest } from 'next/server'
|
|
|
|
const PUBLIC_PATHS = ['/login', '/api/auth', '/api/trpc/stellen.listPublic']
|
|
|
|
export function middleware(request: NextRequest) {
|
|
const pathname = request.nextUrl.pathname
|
|
const isPublic = PUBLIC_PATHS.some((p) => pathname.startsWith(p))
|
|
|
|
if (isPublic) return NextResponse.next()
|
|
|
|
const sessionToken =
|
|
request.cookies.get('better-auth.session_token') ??
|
|
request.cookies.get('__Secure-better-auth.session_token')
|
|
|
|
if (!sessionToken) {
|
|
const loginUrl = new URL('/login', request.url)
|
|
loginUrl.searchParams.set('callbackUrl', pathname)
|
|
return NextResponse.redirect(loginUrl)
|
|
}
|
|
|
|
return NextResponse.next()
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
'/((?!_next/static|_next/image|favicon.ico|uploads).*)',
|
|
],
|
|
}
|