-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmiddleware.ts
More file actions
28 lines (25 loc) · 817 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
28 lines (25 loc) · 817 Bytes
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
import { NextResponse } from 'next/server'
import { kunAuthMiddleware } from '~/middleware/auth'
import { verifyKunCsrf } from '~/middleware/_csrf'
import type { NextRequest } from 'next/server'
export const config = {
matcher: [
'/admin/:path*',
'/user/:path*',
'/comment/:path*',
'/edit/:path*',
// 排除上传路由, 避免 Next.js 缓冲大体积 body (默认 10MB)
// 上传路由在 handler 内部调用 verifyKunCsrf 自行校验
'/api/((?!upload/).*)'
]
}
export const middleware = async (request: NextRequest) => {
if (request.nextUrl.pathname.startsWith('/api')) {
const csrfError = verifyKunCsrf(request)
if (csrfError) {
return NextResponse.json(csrfError, { status: 403 })
}
return NextResponse.next()
}
return kunAuthMiddleware(request)
}