forked from RendaHuang/DaCook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
57 lines (48 loc) · 1.74 KB
/
Copy pathproxy.ts
File metadata and controls
57 lines (48 loc) · 1.74 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { NextResponse, type NextRequest } from 'next/server'
import { jwtVerify } from 'jose'
const COOKIE = 'dacook_session'
const secret = new TextEncoder().encode(
process.env.AUTH_SECRET || 'insecure-dev-secret-change-me',
)
// 无需登录即可访问的页面(是否再跳转交给页面判断——页面能读数据库,能验证用户是否真实存在)
const PUBLIC_PAGES = ['/login', '/setup']
const PUBLIC_API = ['/api/uploads', '/api/cron']
async function hasValidSession(req: NextRequest): Promise<boolean> {
const token = req.cookies.get(COOKIE)?.value
if (!token) return false
try {
await jwtVerify(token, secret)
return true
} catch {
return false
}
}
export async function proxy(req: NextRequest) {
const { pathname } = req.nextUrl
const authed = await hasValidSession(req)
// 公共 API 放行
if (PUBLIC_API.some((p) => pathname.startsWith(p))) {
return NextResponse.next()
}
// 其余 API:未登录返回 401
if (pathname.startsWith('/api')) {
if (!authed)
return NextResponse.json({ error: 'unauthorized' }, { status: 401 })
return NextResponse.next()
}
// 登录/首启页:放行,由页面自行决定是否跳转(避免「有效 cookie + 空库」的重定向死循环)
if (PUBLIC_PAGES.includes(pathname)) {
return NextResponse.next()
}
// 受保护页面:无有效会话跳登录(持有效 cookie 但用户已不存在时,页面的 requireUser 会再处理)
if (!authed) {
return NextResponse.redirect(new URL('/login', req.url))
}
return NextResponse.next()
}
export const config = {
matcher: [
// 排除静态资源
'/((?!_next/static|_next/image|favicon.ico|manifest.json|sw.js|.*\\.(?:png|jpg|jpeg|svg|webp|ico|txt)).*)',
],
}