mirror of
https://github.com/bulwarkmail/native.git
synced 2026-11-02 08:07:11 +00:00
fix: sign out of the last account instead of staying signed in
This commit is contained in:
@@ -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', () => {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user