fix: 修复 AI 设计会话失效后的伪登录

问题:Main 刷新令牌返回 401 后已清空会话,但 Renderer 仍保留用户信息,AI 设计持续返回 AUTH_REQUIRED。

实现:认证初始化等待 Main 同步完成;同步或刷新失败清理登录态;Workspace 识别结构化认证错误并在重新登录后恢复加载;路由守卫响应登录状态变化。

验证:49 个聚焦测试通过,TypeScript、聚焦 Lint 与 Vite 生产构建通过。
This commit is contained in:
2026-08-01 09:37:23 +08:00
parent 1e23028b4b
commit 592bdfbc70
8 changed files with 328 additions and 62 deletions

View File

@@ -123,7 +123,7 @@ function ProtectedLayout({
setupReady: boolean;
}) {
const location = useLocation();
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
const authenticated = useAuthStore((state) => state.isAuthenticated());
const allowsAnonymousImageWorkspace = imageWorkspaceLocalDevelopment
&& (location.pathname === '/'
|| location.pathname === '/image-canvas'
@@ -137,7 +137,7 @@ function ProtectedLayout({
return <StartupScreen />;
}
if (authRequired && !allowsAnonymousImageWorkspace && !isAuthenticated()) {
if (authRequired && !allowsAnonymousImageWorkspace && !authenticated) {
return (
<Navigate
to="/login"
@@ -161,7 +161,7 @@ function ProtectedModuleSelection({
imageWorkspaceLocalDevelopment: boolean;
setupReady: boolean;
}) {
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
const authenticated = useAuthStore((state) => state.isAuthenticated());
if (!setupReady) {
return <StartupScreen />;
@@ -171,7 +171,7 @@ function ProtectedModuleSelection({
return <StartupScreen />;
}
if (authRequired && !imageWorkspaceLocalDevelopment && !isAuthenticated()) {
if (authRequired && !imageWorkspaceLocalDevelopment && !authenticated) {
return <Navigate to="/login" replace state={{ from: AI_MODULE_SELECTION_PATH }} />;
}
@@ -242,14 +242,14 @@ function App() {
const initProviders = useProviderStore((state) => state.init);
const initAuth = useAuthStore((state) => state.init);
const authInitialized = useAuthStore((state) => state.initialized);
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
const authenticated = useAuthStore((state) => state.isAuthenticated());
const bootstrapUserSync = useUserSyncStore((state) => state.bootstrap);
const setupReady = setupComplete || skipSetupForE2E || rendererOnlyPreview;
const authRequired = !skipSetupForE2E && !rendererOnlyPreview;
const imageWorkspaceLocalDevelopment = window.electron?.imageWorkspaceLocalDevelopment === true;
useEffect(() => {
initAuth();
void initAuth();
}, [initAuth]);
useEffect(() => {
@@ -274,9 +274,9 @@ function App() {
if (rendererOnlyPreview) return;
if (!setupReady) return;
if (!authInitialized) return;
if (!isAuthenticated()) return;
if (!authenticated) return;
void bootstrapUserSync();
}, [authInitialized, bootstrapUserSync, isAuthenticated, rendererOnlyPreview, setupReady]);
}, [authenticated, authInitialized, bootstrapUserSync, rendererOnlyPreview, setupReady]);
// Redirect to setup wizard if not complete
useEffect(() => {