-
Notifications
You must be signed in to change notification settings - Fork 13
/
middleware.ts
38 lines (32 loc) · 1.2 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { NextResponse, type NextRequest } from 'next/server'
import { updateSession } from '@/supabase/middleware'
import { Deny, denies } from '@/config/middleware'
// Server Components only have read access to cookies.
// This Middleware example can be used to refresh expired sessions before loading Server Component routes.
export async function middleware(request: NextRequest) {
const { response, authenticated } = await updateSession(request)
const found = denies?.find((deny: Deny) =>
request.nextUrl.pathname.startsWith(deny.source)
)
if (found && found.authenticated === authenticated) {
return NextResponse.redirect(new URL(found.destination, request.url))
}
const url = new URL(request.url)
response.headers.set('x-url', request.url)
response.headers.set('x-origin', url.origin)
response.headers.set('x-pathname', url.pathname)
return response
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/',
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
}