fix: improve account restoration logic and handle stale accounts in auth store

This commit is contained in:
Linus Rath
2026-03-19 19:21:08 +01:00
parent 9b4de4d152
commit ff56245db8
2 changed files with 41 additions and 22 deletions
+8 -5
View File
@@ -28,15 +28,18 @@ export default [
}, },
plugins: { plugins: {
"@typescript-eslint": tseslint, "@typescript-eslint": tseslint,
"react": reactPlugin, react: reactPlugin,
"react-hooks": reactHooksPlugin, "react-hooks": reactHooksPlugin,
}, },
rules: { rules: {
...tseslint.configs.recommended.rules, ...tseslint.configs.recommended.rules,
"@typescript-eslint/no-unused-vars": ["warn", { "@typescript-eslint/no-unused-vars": [
argsIgnorePattern: "^_", "warn",
varsIgnorePattern: "^_" {
}], argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
},
],
"@typescript-eslint/no-explicit-any": "warn", "@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-empty-object-type": "off", "@typescript-eslint/no-empty-object-type": "off",
"react-hooks/rules-of-hooks": "error", "react-hooks/rules-of-hooks": "error",
+33 -17
View File
@@ -741,23 +741,39 @@ export const useAuthStore = create<AuthState>()(
} }
} catch (err) { } catch (err) {
debug.error(`Failed to restore client for ${accountId}:`, err); debug.error(`Failed to restore client for ${accountId}:`, err);
accountStore.updateAccount(accountId, {
isConnected: false,
hasError: true,
errorMessage: err instanceof Error ? err.message : 'Connection failed',
});
set({ isLoading: false });
return;
} }
} }
if (!targetClient) { if (!targetClient) {
accountStore.updateAccount(accountId, { // Cannot restore — remove the stale account and redirect to login
isConnected: false, evictAccount(accountId);
hasError: true, accountStore.removeAccount(accountId);
errorMessage: 'Unable to restore session', fetch(`/api/auth/session?slot=${targetAccount.cookieSlot}`, { method: 'DELETE' }).catch(() => {});
});
// Restore the previous account if still available
if (state.activeAccountId && state.activeAccountId !== accountId) {
const prevClient = clients.get(state.activeAccountId);
const prevAccount = accountStore.getAccountById(state.activeAccountId);
if (prevClient && prevAccount) {
restoreAccount(state.activeAccountId);
accountStore.setActiveAccount(state.activeAccountId);
set({
isLoading: false,
serverUrl: prevAccount.serverUrl,
username: prevAccount.username,
client: prevClient,
authMode: prevAccount.authMode,
rememberMe: prevAccount.rememberMe,
connectionLost: false,
activeAccountId: state.activeAccountId,
});
return;
}
}
set({ isLoading: false }); set({ isLoading: false });
// Redirect to login so the user can re-authenticate
replaceWindowLocation(getLocaleLoginPath());
return; return;
} }
@@ -865,11 +881,11 @@ export const useAuthStore = create<AuthState>()(
} }
} catch (err) { } catch (err) {
debug.error(`Failed to restore account ${account.id}:`, err); debug.error(`Failed to restore account ${account.id}:`, err);
accountStore.updateAccount(account.id, { // Remove unrestorable accounts so the user is prompted to log in
isConnected: false, // again rather than seeing a stale error entry forever.
hasError: true, evictAccount(account.id);
errorMessage: err instanceof Error ? err.message : 'Restore failed', accountStore.removeAccount(account.id);
}); fetch(`/api/auth/session?slot=${account.cookieSlot}`, { method: 'DELETE' }).catch(() => {});
} }
} }