fix: sign out of the last account instead of staying signed in

This commit is contained in:
Linus Rath
2026-10-22 22:04:28 +02:00
parent e06d92d88f
commit 721c2e34a1
2 changed files with 30 additions and 3 deletions
+25
View File
@@ -129,6 +129,31 @@ describe('auth-store', () => {
expect(state.serverUrl).toBeNull();
expect(state.username).toBeNull();
});
// logout used to read the account list from a snapshot taken before it
// removed the account: the removed account still looked like one to
// switch to, the "switch" to the active account returned at once, and
// signing out of the only account left the app signed in.
it('signs out of the only account', async () => {
const id = 'me@mail.example.com';
useAccountStore.setState({
accounts: [{
id, serverUrl: 'https://mail.example.com', username: 'me',
displayName: 'me', email: 'me', avatarColor: '#000', lastLoginAt: 0,
isConnected: true, hasError: false, isDefault: true,
}],
activeAccountId: id,
defaultAccountId: id,
});
useAuthStore.setState({ isAuthenticated: true, activeAccountId: id, serverUrl: 'x', username: 'y' });
await useAuthStore.getState().logout();
expect(useAccountStore.getState().accounts).toEqual([]);
const state = useAuthStore.getState();
expect(state.isAuthenticated).toBe(false);
expect(state.activeAccountId).toBeNull();
});
});
describe('mail the viewer held in memory', () => {
+5 -3
View File
@@ -602,10 +602,12 @@ export const useAuthStore = create<AuthState>((set, get) => ({
jmapClient.reset();
clearAccountFeatureStores(currentId);
// Switch to next remaining account, if any
const remaining = accountStore.accounts;
// Switch to next remaining account, if any. Read the store again:
// `accountStore` is the state from before removeAccount.
const after = useAccountStore.getState();
const remaining = after.accounts.filter((a) => a.id !== currentId);
if (remaining.length > 0) {
const next = accountStore.getDefaultAccount() ?? remaining[0];
const next = after.getDefaultAccount() ?? remaining[0];
try {
await get().switchAccount(next.id);
return;