fix: restore lovart home client session state
Some checks failed
Production Build / build (push) Has been cancelled
Dependency Audit / audit (push) Has been cancelled
Quality Gates / quality (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled

This commit is contained in:
Your Name 2026-05-27 18:23:37 +08:00
parent 4a976bb749
commit 85981acb8d
2 changed files with 33 additions and 4 deletions

View File

@ -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: {

View File

@ -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<HTMLDivElement | null>(null);
const filesRef = useRef<UploadFileItem[]>([]);
const { data: sessionData, mutate: mutateSession } = useSWR<HomeSessionResponse>(
"/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("登录失败,请稍后重试");