perf(session): ttl

This commit is contained in:
zhangheng
2026-04-23 14:47:14 +08:00
parent cd1bad6da9
commit 5b38f684c3
6 changed files with 488 additions and 222 deletions

View File

@@ -1,4 +1,7 @@
import { cookies } from "next/headers"
import {
getSessionRedisKeyFromCookieValue,
SESSION_COOKIE_NAME,
} from "@/components/login/libs/session-common"
import { NextRequest, NextResponse } from "next/server"
const publicRoutes = ["/login", "/"]
@@ -22,11 +25,10 @@ export default async function middleware(req: NextRequest) {
// const isProtectedRoute = protectedRoutes.includes(path)
const isPublicRoute = publicRoutes.includes(path)
// 3. Decrypt the session from the cookie
const cookie = (await cookies()).get("session")?.value
const session = cookie ? JSON.parse(cookie) : ""
// 3. Resolve the session key from the cookie
const session = getSessionRedisKeyFromCookieValue(
req.cookies.get(SESSION_COOKIE_NAME)?.value
)
// 4. Redirect to /login if the user is not authenticated
if (!isPublicRoute && !session) {