-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
30 lines (26 loc) · 1.18 KB
/
middleware.js
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
import { NextResponse } from 'next/server'
import { auth } from '@/auth'
const basePath = process.env.BASE_PATH ?? ''
const restrictedLabels = new Set(process.env.RESTRICTED_LABELS?.split(',') ?? [])
export default auth((request) => {
if (request.method === 'POST') // Do not intercept next.js requests
return
const labels = request.nextUrl.searchParams.get('-filt.labels')?.split(',') ?? []
if (request.nextUrl.pathname.startsWith('/history') || labels.filter(Set.prototype.has, restrictedLabels).length > 0) {
if (!request.auth) {
const callbackUrl=`${basePath}${request.nextUrl.pathname}${request.nextUrl.search}`
const url = new URL(`${basePath}/api/auth/signin?callbackUrl=${encodeURIComponent(callbackUrl)}`, request.nextUrl.origin)
return Response.redirect(url)
}
} else if (request.nextUrl.pathname.toLowerCase().endsWith('.jpg')) {
const url = request.nextUrl.clone()
url.pathname = '/image' + request.nextUrl.pathname
return NextResponse.rewrite(url)
}
})
export const config = {
matcher: [
'/',
'/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)'
]
}