fix(auth): 访客工作台 - 欢迎页关闭后展示创作界面 (#697)
Co-authored-by: a0 <a0@Mac-mini.local>
This commit is contained in:
@@ -25,7 +25,7 @@ export function AuthSessionHydrator({ children }: { children: ReactNode }) {
|
||||
durationMs: performance.now() - startedAt,
|
||||
});
|
||||
if (!payload.user) {
|
||||
applyAnonymousSession(payload);
|
||||
await applyAnonymousSession(payload);
|
||||
recordDiagnosticEvent({
|
||||
category: "navigation",
|
||||
level: "info",
|
||||
@@ -48,9 +48,9 @@ export function AuthSessionHydrator({ children }: { children: ReactNode }) {
|
||||
});
|
||||
preloadWorkspaceRoute(window.location.pathname);
|
||||
})
|
||||
.catch(() => {
|
||||
.catch(async () => {
|
||||
if (!cancelled) {
|
||||
applyAnonymousSession({ user: null });
|
||||
await applyAnonymousSession({ user: null });
|
||||
recordDiagnosticEvent({
|
||||
category: "navigation",
|
||||
level: "warning",
|
||||
@@ -68,11 +68,7 @@ export function AuthSessionHydrator({ children }: { children: ReactNode }) {
|
||||
return hydrated ? children : <FullScreenLoader />;
|
||||
}
|
||||
|
||||
function applyAnonymousSession(payload: AuthSessionPayload) {
|
||||
const store = useUserStore.getState();
|
||||
store.clearSession();
|
||||
store.setRuntimeLimits(payload.runtimeLimits);
|
||||
store.setDrawingEngine(payload.drawingEngine);
|
||||
store.setFeatures(payload.features);
|
||||
store.setHydrated(true);
|
||||
async function applyAnonymousSession(payload: AuthSessionPayload) {
|
||||
const { applyUserSession } = await import("@/lib/user-session");
|
||||
await applyUserSession({ ...payload, user: null });
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@ import { Navigate, useLocation } from "react-router";
|
||||
import { FullScreenLoader } from "@/components/ui/aceternity/full-screen-loader";
|
||||
import { useUserStore } from "@/stores/use-user-store";
|
||||
|
||||
export function RequireAuth({ children }: { children: ReactNode }) {
|
||||
export function RequireAuth({ children, allowAnonymous = false }: { children: ReactNode; allowAnonymous?: boolean }) {
|
||||
const location = useLocation();
|
||||
const hydrated = useUserStore((state) => state.hydrated);
|
||||
const user = useUserStore((state) => state.user);
|
||||
|
||||
if (!hydrated) return <FullScreenLoader />;
|
||||
if (!user) return <Navigate to={`/login?next=${encodeURIComponent(location.pathname + location.search)}`} replace />;
|
||||
if (!user && !allowAnonymous) return <Navigate to={`/login?next=${encodeURIComponent(location.pathname + location.search)}`} replace />;
|
||||
return children;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { Popover } from "antd";
|
||||
import { Switch } from "@/components/ui/base/switch";
|
||||
import { LogIn, Moon, Sun } from "lucide-react";
|
||||
import { CircleUserRound, Moon, Sun } from "lucide-react";
|
||||
import { useState } from "react";
|
||||
import { Link } from "react-router";
|
||||
|
||||
import { AppChangelogButton } from "@/components/layout/app-changelog-modal";
|
||||
import { WorkspaceAccountCard } from "./workspace-account-card";
|
||||
@@ -51,9 +50,5 @@ export function WorkspaceAccountMenu() {
|
||||
<UserAvatar user={user} className="size-6" />
|
||||
</button>
|
||||
</Popover></>
|
||||
) : (
|
||||
<Link to="/login" className="app-workspace-topbar-icon-button" aria-label="登录" title="登录">
|
||||
<LogIn />
|
||||
</Link>
|
||||
);
|
||||
) : <span className="inline-flex items-center gap-1.5 px-1.5 text-xs text-foreground/55" role="img" aria-label="游客模式" title="游客模式"><CircleUserRound className="size-5" /><span className="hidden sm:inline">游客</span></span>;
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ function WorkspaceSidebarProfile({ collapsed, user }: { collapsed: boolean; user
|
||||
useEffect(() => setFailed(false), [avatarUrl]);
|
||||
|
||||
if (!user) {
|
||||
return <Link to="/login" className={cn("app-workspace-sidebar-profile", collapsed && "is-collapsed")} aria-label="登录" title="登录"><CircleUserRound className="size-5" /><span>登录</span></Link>;
|
||||
return <div className={cn("app-workspace-sidebar-profile", collapsed && "is-collapsed")} role="img" aria-label="游客模式" title="游客模式"><span className="app-workspace-sidebar-profile-avatar"><CircleUserRound aria-hidden /></span>{!collapsed ? <span>游客模式</span> : null}</div>;
|
||||
}
|
||||
|
||||
const avatar = avatarUrl && !failed ? <img src={avatarUrl} alt="" referrerPolicy="no-referrer" onError={() => setFailed(true)} /> : <CircleUserRound aria-hidden />;
|
||||
|
||||
Reference in New Issue
Block a user