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

@@ -46,10 +46,11 @@ type AuthState = {
tokenType: string | null;
expiresAt: number | null;
user: AuthUser | null;
init: () => void;
init: () => Promise<void>;
loginWithBrowser: () => Promise<void>;
refreshSession: () => Promise<string | null>;
getValidAccessToken: () => Promise<string | null>;
invalidateSession: (message?: string) => void;
logout: () => Promise<void>;
isAuthenticated: () => boolean;
};
@@ -120,10 +121,10 @@ async function syncMainSession(session: {
refreshToken: string | null;
tokenType: string | null;
expiresAt: number | null;
}): Promise<void> {
if (!session.accessToken) return;
}): Promise<boolean> {
if (!session.accessToken) return false;
try {
await hostApiFetch<AuthActionResponse>('/api/auth/session/sync', {
const response = await hostApiFetch<AuthActionResponse>('/api/auth/session/sync', {
method: 'POST',
body: JSON.stringify({
accessToken: session.accessToken,
@@ -132,8 +133,9 @@ async function syncMainSession(session: {
expiresAt: session.expiresAt,
}),
});
return response.success;
} catch {
// The renderer session remains authoritative for UI; Main can resync later.
return false;
}
}
@@ -147,8 +149,9 @@ export const useAuthStore = create<AuthState>()(
clientId: DEFAULT_CLIENT_ID,
...getClearedSession(),
init: () => {
init: async () => {
const state = get();
if (state.loading && !state.initialized) return;
const authBaseChanged = Boolean(
state.authBase
&& trimTrailingSlash(state.authBase) !== trimTrailingSlash(DEFAULT_AUTH_BASE),
@@ -159,21 +162,36 @@ export const useAuthStore = create<AuthState>()(
&& state.expiresAt <= Date.now() + TOKEN_EXPIRY_SKEW_MS,
);
set({
initialized: true,
initialized: false,
loading: false,
error: null,
authBase: DEFAULT_AUTH_BASE,
clientId: DEFAULT_CLIENT_ID,
...(expired || authBaseChanged ? getClearedSession() : {}),
});
if (!expired && !authBaseChanged && state.accessToken) {
void syncMainSession({
accessToken: state.accessToken,
refreshToken: state.refreshToken,
tokenType: state.tokenType,
expiresAt: state.expiresAt,
});
if (expired || authBaseChanged || !state.accessToken) {
set({ initialized: true });
return;
}
set({ loading: true });
const synchronized = await syncMainSession({
accessToken: state.accessToken,
refreshToken: state.refreshToken,
tokenType: state.tokenType,
expiresAt: state.expiresAt,
});
if (!synchronized) {
set({
initialized: true,
loading: false,
error: '登录状态恢复失败,请重新登录。',
...getClearedSession(),
});
return;
}
set({ initialized: true, loading: false });
},
loginWithBrowser: async () => {
@@ -199,7 +217,9 @@ export const useAuthStore = create<AuthState>()(
clientId,
...session,
});
await syncMainSession(session);
if (!await syncMainSession(session)) {
throw new Error('Failed to synchronize the signed-in session');
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
set({ loading: false, error: message, ...getClearedSession() });
@@ -228,11 +248,18 @@ export const useAuthStore = create<AuthState>()(
error: null,
...session,
});
await syncMainSession(session);
if (!await syncMainSession(session)) {
throw new Error('Failed to synchronize the refreshed session');
}
return session.accessToken;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
set({ loading: false, error: message });
set({
initialized: true,
loading: false,
error: message,
...getClearedSession(),
});
return null;
}
},
@@ -252,6 +279,15 @@ export const useAuthStore = create<AuthState>()(
return accessToken;
},
invalidateSession: (message = '登录已过期,请重新登录。') => {
set({
initialized: true,
loading: false,
error: message,
...getClearedSession(),
});
},
logout: async () => {
const { accessToken } = get();
try {