From 85981acb8d84bf255cf0e750146dda741b862ade Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 27 May 2026 18:23:37 +0800 Subject: [PATCH] fix: restore lovart home client session state --- app/api/auth/validate-session/route.ts | 14 ++++++++++++-- components/home/lovart-home.tsx | 23 +++++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/api/auth/validate-session/route.ts b/app/api/auth/validate-session/route.ts index c2cea4dc0..82234a0ff 100644 --- a/app/api/auth/validate-session/route.ts +++ b/app/api/auth/validate-session/route.ts @@ -3,11 +3,21 @@ import { auth } from "@/app/(auth)/auth"; export async function GET() { const session = await auth().catch(() => null); + const user = session?.user + ? { + id: session.user.id, + email: session.user.email ?? null, + name: session.user.name ?? null, + image: session.user.image ?? null, + type: session.user.type ?? null, + } + : null; return NextResponse.json( { - valid: Boolean(session?.user?.id), - userType: session?.user?.type ?? null, + valid: Boolean(user?.id), + user, + userType: user?.type ?? null, }, { headers: { diff --git a/components/home/lovart-home.tsx b/components/home/lovart-home.tsx index 1b292c9ec..60671a2e7 100644 --- a/components/home/lovart-home.tsx +++ b/components/home/lovart-home.tsx @@ -238,6 +238,12 @@ type LovartHomeProps = { heroSubtitleText?: string | null; }; +type HomeSessionResponse = { + valid: boolean; + user?: HomeUser | null; + userType?: string | null; +}; + type UploadFileItem = { id: string; file: File; @@ -406,8 +412,8 @@ function AccountMenuItem({ } export function LovartHome({ - user = null, - isAuthenticated = false, + user: initialUser = null, + isAuthenticated: initialIsAuthenticated = false, siteName: initialSiteName = "AI Chatbot", siteLogo: initialSiteLogo = null, siteLogoDark: initialSiteLogoDark = null, @@ -468,6 +474,18 @@ export function LovartHome({ const featuredLoadMoreRef = useRef(null); const filesRef = useRef([]); + const { data: sessionData, mutate: mutateSession } = useSWR( + "/api/auth/validate-session", + fetcher, + { + dedupingInterval: 5000, + revalidateOnFocus: true, + revalidateOnReconnect: true, + } + ); + const user = sessionData?.user ?? initialUser; + const isAuthenticated = sessionData?.valid ?? initialIsAuthenticated; + useEffect(() => { if (typeof window === "undefined") { return; @@ -1115,6 +1133,7 @@ export function LovartHome({ return; } + await mutateSession(); window.location.href = "/"; } catch (_error) { toast.error("登录失败,请稍后重试");