-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
executable file
·46 lines (37 loc) · 1.24 KB
/
middleware.ts
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
import { type NextRequest, NextResponse } from "next/server"
export function middleware(request: NextRequest) {
const path = request.nextUrl.pathname
// 定义需要认证的路径
const protectedPaths = ["/dashboard"]
// 检查当前路径是否需要认证
const isProtectedPath = protectedPaths.some(
(protectedPath) => path === protectedPath || path.startsWith(`${protectedPath}/`),
)
if (isProtectedPath) {
// 获取会话 Cookie
const sessionCookie = request.cookies.get("session")
// 如果没有会话 Cookie,重定向到首页
if (!sessionCookie) {
return NextResponse.redirect(new URL("/", request.url))
}
try {
// 解析会话 Cookie
const session = JSON.parse(sessionCookie.value)
// 检查会话是否过期
if (session.expiresAt && session.expiresAt < Date.now()) {
// 如果会话过期,重定向到首页
return NextResponse.redirect(new URL("/", request.url))
}
} catch (error) {
// 如果解析会话 Cookie 失败,重定向到首页
return NextResponse.redirect(new URL("/", request.url))
}
}
return NextResponse.next()
}
export const config = {
matcher: [
// 需要认证的路径
"/dashboard/:path*",
],
}