fix: restore lovart home client session state
This commit is contained in:
parent
4a976bb749
commit
85981acb8d
@ -3,11 +3,21 @@ import { auth } from "@/app/(auth)/auth";
|
|||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
const session = await auth().catch(() => null);
|
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(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
valid: Boolean(session?.user?.id),
|
valid: Boolean(user?.id),
|
||||||
userType: session?.user?.type ?? null,
|
user,
|
||||||
|
userType: user?.type ?? null,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@ -238,6 +238,12 @@ type LovartHomeProps = {
|
|||||||
heroSubtitleText?: string | null;
|
heroSubtitleText?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type HomeSessionResponse = {
|
||||||
|
valid: boolean;
|
||||||
|
user?: HomeUser | null;
|
||||||
|
userType?: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
type UploadFileItem = {
|
type UploadFileItem = {
|
||||||
id: string;
|
id: string;
|
||||||
file: File;
|
file: File;
|
||||||
@ -406,8 +412,8 @@ function AccountMenuItem({
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function LovartHome({
|
export function LovartHome({
|
||||||
user = null,
|
user: initialUser = null,
|
||||||
isAuthenticated = false,
|
isAuthenticated: initialIsAuthenticated = false,
|
||||||
siteName: initialSiteName = "AI Chatbot",
|
siteName: initialSiteName = "AI Chatbot",
|
||||||
siteLogo: initialSiteLogo = null,
|
siteLogo: initialSiteLogo = null,
|
||||||
siteLogoDark: initialSiteLogoDark = null,
|
siteLogoDark: initialSiteLogoDark = null,
|
||||||
@ -468,6 +474,18 @@ export function LovartHome({
|
|||||||
const featuredLoadMoreRef = useRef<HTMLDivElement | null>(null);
|
const featuredLoadMoreRef = useRef<HTMLDivElement | null>(null);
|
||||||
const filesRef = useRef<UploadFileItem[]>([]);
|
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(() => {
|
useEffect(() => {
|
||||||
if (typeof window === "undefined") {
|
if (typeof window === "undefined") {
|
||||||
return;
|
return;
|
||||||
@ -1115,6 +1133,7 @@ export function LovartHome({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await mutateSession();
|
||||||
window.location.href = "/";
|
window.location.href = "/";
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
toast.error("登录失败,请稍后重试");
|
toast.error("登录失败,请稍后重试");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user