Skip to content

Commit fdaa878

Browse files
committed
fix: drop late user and settings responses after the session ends (TM-852)
A user response that lands after logout no longer restores the previous account, a settings response from an earlier session is ignored, and the previous account's UI settings are reset when the user is forgotten.
1 parent 50e3ee7 commit fdaa878

5 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎src/actions/tmgr/__tests__/getUserStale.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ jest.mock('@/utils/requestCache', () => ({
1616
}));
1717
jest.mock('@/utils/timezoneDetect', () => ({ detectUserTimeZone: jest.fn() }));
1818

19+
import axios from '@/plugins/axios';
1920
import store from '@/store';
2021
import requestCache from '@/utils/requestCache';
2122
import { detectUserTimeZone } from '@/utils/timezoneDetect';
22-
import { getUser } from '../user';
23+
import { getUser, getUserSettings } from '../user';
2324

2425
const user = {
2526
id: 2,
@@ -47,3 +48,20 @@ test('a response that outlived its session has no effects', async () => {
4748
expect(store.commit).not.toHaveBeenCalled();
4849
expect(detectUserTimeZone).not.toHaveBeenCalled();
4950
});
51+
52+
test('user settings from the current session are applied', async () => {
53+
(axios.get as jest.Mock).mockResolvedValue({
54+
data: { data: { settings: { a: 1 } } },
55+
});
56+
await getUserSettings();
57+
expect(store.commit).toHaveBeenCalledWith('setUserSettings', { a: 1 });
58+
});
59+
60+
test('user settings that outlived their session are dropped', async () => {
61+
(axios.get as jest.Mock).mockImplementation(async () => {
62+
(store.state as any).sessionGeneration = 2;
63+
return { data: { data: { settings: { a: 1 } } } };
64+
});
65+
await getUserSettings();
66+
expect(store.commit).not.toHaveBeenCalled();
67+
});

‎src/actions/tmgr/user.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,13 @@ export const updateUser = async (
140140
export interface UserSettings {}
141141

142142
export const getUserSettings = async () => {
143+
const sessionGeneration = store.state.sessionGeneration;
143144
const {
144145
data: { data },
145146
} = await $axios.get('user/settings');
146147

148+
if (store.state.sessionGeneration !== sessionGeneration) return;
149+
147150
if (data?.settings) {
148151
store.commit('setUserSettings', data.settings);
149152
}

‎src/store/__tests__/clientWorkspace.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function fixture(
6363
},
6464
storages,
6565
).default;
66+
config.state.token = { token: 'opaque' };
6667
return { config, getWorkspaces, cache };
6768
}
6869

‎src/store/__tests__/sessionIdentity.test.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ describe('setToken forgets the cloud user', () => {
9696
expect(state.workspacesById).toEqual({});
9797
});
9898

99+
it('resets user settings on logout', () => {
100+
const { state, mutations } = signedIn(2, tokenFor('2'), {
101+
userSettings: { showTooltips: false, pomodoro: 1 },
102+
});
103+
mutations.setToken(state, null);
104+
expect(state.userSettings).toEqual({ showTooltips: true });
105+
});
106+
107+
it('resets user settings when another account takes over', () => {
108+
const { state, mutations } = signedIn(2, tokenFor('2'), {
109+
userSettings: { showTooltips: false, pomodoro: 1 },
110+
});
111+
mutations.setToken(state, tokenFor('5'));
112+
expect(state.userSettings).toEqual({ showTooltips: true });
113+
});
114+
99115
it('keeps the user on a same-subject rotation', () => {
100116
const { state, mutations, disconnectRealtime } = signedIn(2, tokenFor('2'));
101117
mutations.setToken(state, { token: fakeJwt('2') + 'x' });
@@ -162,10 +178,17 @@ describe('setUser ignores a late response for the previous account', () => {
162178
expect(state.user.id).toBe(2);
163179
});
164180

165-
it('accepts any id when there is no token', () => {
166-
const { state, mutations } = signedIn(5, null);
181+
it('ignores a late cloud user after logout', () => {
182+
const { state, mutations } = signedIn(2, tokenFor('2'));
183+
mutations.setToken(state, null);
167184
mutations.setUser(state, { id: 2, settings: [] });
168-
expect(state.user.id).toBe(2);
185+
expect(state.user.id).toBeUndefined();
186+
});
187+
188+
it('accepts a local profile payload when there is no token', () => {
189+
const { state, mutations } = signedIn(5, null);
190+
mutations.setUser(state, { id: -1, settings: [] });
191+
expect(state.user.id).toBe(-1);
169192
});
170193

171194
it('accepts a local profile payload under a token', () => {

‎src/store/index.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const forgetCloudUser = (state, { bump = true } = {}) => {
7070
if (bump) state.sessionGeneration++;
7171
state.user = {};
7272
state.userSettingsMap = {};
73+
state.userSettings = { showTooltips: true };
7374
state.defaultWorkspaceId = null;
7475
state.clientWorkspaceId = null;
7576
state.workspaces = [];
@@ -298,6 +299,7 @@ const mutations = {
298299
setUser(state, user) {
299300
const subject = tokenSubject(state.token);
300301
if (subject != null && user?.id > 0 && user.id !== subject) return;
302+
if (state.token == null && user?.id > 0) return;
301303
if (state.user?.id !== user?.id) {
302304
state.sessionGeneration++;
303305
requestCache.clear();

0 commit comments

Comments
 (0)