-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
31 lines (24 loc) · 976 Bytes
/
middleware.ts
File metadata and controls
31 lines (24 loc) · 976 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
29
30
31
import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
import { getToken } from "next-auth/jwt"
// 需要登录才能访问的路径
const protectedPaths = ["/submit", "/suggestions/submit"]
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// 检查当前路径是否需要保护
const isProtectedPath = protectedPaths.some((path) => pathname === path || pathname.startsWith(`${path}/`))
if (isProtectedPath) {
const token = await getToken({ req: request })
// 如果用户未登录,重定向到登录页面
if (!token) {
const url = new URL(`/auth/signin`, request.url)
url.searchParams.set("callbackUrl", encodeURI(pathname))
return NextResponse.redirect(url)
}
}
return NextResponse.next()
}
// 配置匹配的路径
export const config = {
matcher: ["/submit", "/submit/:path*", "/suggestions/submit", "/suggestions/submit/:path*"],
}