-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
39 lines (31 loc) · 1.24 KB
/
Copy pathproxy.ts
File metadata and controls
39 lines (31 loc) · 1.24 KB
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
39
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
const NO_STORE_PATHS = ['/login', '/register', '/']
const AUTH_DISABLED = process.env.AUTH_DISABLED === '1' || process.env.AUTH_DISABLED === 'true'
// 与 lib/auth/auth.ts 的 ACCESS_COOKIE_NAME / REFRESH_COOKIE_NAME 保持同步
const ACCESS_COOKIE = 'vlm-access-token'
const REFRESH_COOKIE = 'vlm-refresh-token'
const REQUEST_PATH_HEADER = 'x-request-path'
export function proxy(request: NextRequest) {
const { pathname, search } = request.nextUrl
const requestHeaders = new Headers(request.headers)
requestHeaders.set(REQUEST_PATH_HEADER, pathname + search)
if (
pathname.startsWith('/dashboard') &&
!AUTH_DISABLED &&
!request.cookies.has(ACCESS_COOKIE) &&
!request.cookies.has(REFRESH_COOKIE)
) {
const loginUrl = new URL('/login', request.url)
loginUrl.searchParams.set('next', pathname + search)
return NextResponse.redirect(loginUrl, 302)
}
const response = NextResponse.next({ request: { headers: requestHeaders } })
if (NO_STORE_PATHS.includes(pathname)) {
response.headers.set('Cache-Control', 'no-store')
}
return response
}
export const config = {
matcher: ['/login', '/register', '/', '/dashboard/:path*'],
}