chore: middleware hernoemd naar proxy (Next.js 16)
- middleware.ts → proxy.ts - export function middleware → proxy - docs/patterns/middleware.md bijgewerkt Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
b4371f5afb
commit
703a912310
2 changed files with 43 additions and 12 deletions
29
proxy.ts
Normal file
29
proxy.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { NextResponse } from 'next/server'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { sessionOptions } from '@/lib/session'
|
||||
|
||||
const protectedRoutes = ['/dashboard', '/products', '/todos', '/settings']
|
||||
const authRoutes = ['/login', '/register']
|
||||
|
||||
export function proxy(request: NextRequest) {
|
||||
const path = request.nextUrl.pathname
|
||||
const isProtected = protectedRoutes.some(r => path.startsWith(r))
|
||||
const isAuthRoute = authRoutes.some(r => path.startsWith(r))
|
||||
|
||||
// Check cookie existence only — full session validation happens in layout.tsx
|
||||
const hasSession = !!request.cookies.get(sessionOptions.cookieName)?.value
|
||||
|
||||
if (isProtected && !hasSession) {
|
||||
return NextResponse.redirect(new URL('/login', request.url))
|
||||
}
|
||||
|
||||
if (isAuthRoute && hasSession) {
|
||||
return NextResponse.redirect(new URL('/dashboard', request.url))
|
||||
}
|
||||
|
||||
return NextResponse.next()
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue