Skip to content

Commit 33ca0c8

Browse files
refactor(frontend): route signups-closed toast through logout msg
`signOut` ultimately calls `window.location.reload()`, so a toast shown right before it would never reach the user. Mirror the `errorSignOut`/`warnSignOut` pattern with a new `infoSignOut` helper that passes the message through `logout({ msg })`, letting it survive the reload via `displayAndCleanLogoutMsg`.
1 parent bbd43e3 commit 33ca0c8

4 files changed

Lines changed: 72 additions & 22 deletions

File tree

src/frontend/src/lib/services/auth.services.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,25 @@ export const errorSignOut = (text: string): Promise<void> => {
139139
});
140140
};
141141

142+
export const infoSignOut = ({
143+
text,
144+
source = ''
145+
}: {
146+
text: string;
147+
source?: string;
148+
}): Promise<void> => {
149+
trackSignOut({
150+
name: TRACK_SIGN_OUT_SUCCESS,
151+
meta: { reason: 'info', text, source }
152+
});
153+
return logout({
154+
msg: {
155+
text,
156+
level: 'info'
157+
}
158+
});
159+
};
160+
142161
export const warnSignOut = (text: string): Promise<void> => {
143162
trackSignOut({
144163
name: TRACK_SIGN_OUT_WITH_WARNING,

src/frontend/src/lib/services/loader.services.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ import {
1111
} from '$lib/derived/networks.derived';
1212
import { loadAddresses } from '$lib/services/addresses.services';
1313
import { trackRateLimited } from '$lib/services/analytics.services';
14-
import { errorSignOut, nullishSignOut, signOut } from '$lib/services/auth.services';
14+
import {
15+
errorSignOut,
16+
infoSignOut,
17+
nullishSignOut,
18+
signOut
19+
} from '$lib/services/auth.services';
1520
import { loadUserProfile } from '$lib/services/load-user-profile.services';
1621
import { authStore } from '$lib/stores/auth.store';
1722
import { i18n } from '$lib/stores/i18n.store';
18-
import { toastsShow } from '$lib/stores/toasts.store';
1923
import type { NullishIdentity } from '$lib/types/identity';
2024
import type { NetworkId } from '$lib/types/network';
2125
import type { ResultSuccess } from '$lib/types/utils';
@@ -92,20 +96,14 @@ export const initLoader = async ({
9296
});
9397

9498
if (!userProfileSuccess) {
95-
const signupsClosed = userProfileError === 'signups-closed';
96-
97-
if (signupsClosed) {
98-
toastsShow({
99+
if (userProfileError === 'signups-closed') {
100+
await infoSignOut({
99101
text: get(i18n).auth.info.signups_closed,
100-
level: 'info'
102+
source: 'signups-closed'
101103
});
104+
return;
102105
}
103-
104-
await signOut({
105-
resetUrl: signupsClosed,
106-
source: signupsClosed ? 'signups-closed' : ''
107-
});
108-
106+
await signOut({});
109107
return;
110108
}
111109

src/frontend/src/tests/lib/services/auth.services.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { trackEvent } from '$lib/services/analytics.services';
99
import {
1010
errorSignOut,
1111
idleSignOut,
12+
infoSignOut,
1213
lockSession,
1314
nullishSignOut,
1415
signOut,
@@ -417,6 +418,42 @@ describe('auth.services', () => {
417418
});
418419
});
419420

421+
describe('infoSignOut', () => {
422+
const mockText = 'New sign-ups are currently restricted.';
423+
424+
beforeEach(() => {
425+
vi.clearAllMocks();
426+
});
427+
428+
it('should track the sign out event with the provided source', async () => {
429+
await infoSignOut({ text: mockText, source: 'signups-closed' });
430+
431+
expect(trackEvent).toHaveBeenCalledExactlyOnceWith({
432+
name: TRACK_SIGN_OUT_SUCCESS,
433+
metadata: {
434+
reason: 'info',
435+
level: '',
436+
text: mockText,
437+
source: 'signups-closed',
438+
resetUrl: 'false',
439+
clearStorages: 'true'
440+
}
441+
});
442+
});
443+
444+
it('should append the message to the reload URL with level=info', async () => {
445+
await infoSignOut({ text: mockText });
446+
447+
expect(window.history.replaceState).toHaveBeenCalledExactlyOnceWith(
448+
{},
449+
'',
450+
new URL(
451+
`${rootLocation}?msg=${encodeURI(encodeURI(mockText))}&level=info&${PARAM_DELETE_IDB_CACHE}=true`
452+
)
453+
);
454+
});
455+
});
456+
420457
describe('nullishSignOut', () => {
421458
const expectedText = en.auth.warning.not_signed_in;
422459

src/frontend/src/tests/lib/services/loader.services.spec.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import { ZERO } from '$lib/constants/app.constants';
88
import { loadAddresses } from '$lib/services/addresses.services';
99
import { trackRateLimited } from '$lib/services/analytics.services';
1010
import * as authServices from '$lib/services/auth.services';
11-
import { nullishSignOut, signOut } from '$lib/services/auth.services';
11+
import { infoSignOut, nullishSignOut, signOut } from '$lib/services/auth.services';
1212
import { loadUserProfile } from '$lib/services/load-user-profile.services';
1313
import { initLoader, initSignerAllowance } from '$lib/services/loader.services';
1414
import { authStore } from '$lib/stores/auth.store';
15-
import * as toastsStore from '$lib/stores/toasts.store';
1615
import { userProfileStore } from '$lib/stores/user-profile.store';
1716
import type { AllowSigningOutcome } from '$lib/types/api';
1817
import { mockAuthStore } from '$tests/mocks/auth.mock';
@@ -138,6 +137,7 @@ describe('loader.services', () => {
138137
vi.resetAllMocks();
139138

140139
vi.spyOn(authServices, 'signOut').mockImplementation(vi.fn());
140+
vi.spyOn(authServices, 'infoSignOut').mockImplementation(vi.fn());
141141
vi.spyOn(authServices, 'nullishSignOut').mockImplementation(vi.fn());
142142
vi.spyOn(api, 'allowSigning').mockResolvedValue(mockExecutedOutcome);
143143

@@ -168,23 +168,19 @@ describe('loader.services', () => {
168168
expect(signOut).toHaveBeenCalledOnce();
169169
});
170170

171-
it('should show an info toast and sign out when signups are closed', async () => {
172-
const toastsShowSpy = vi.spyOn(toastsStore, 'toastsShow');
171+
it('should sign out via infoSignOut when signups are closed', async () => {
173172
vi.mocked(loadUserProfile).mockResolvedValueOnce({
174173
success: false,
175174
err: 'signups-closed'
176175
});
177176

178177
await initLoader(mockParams);
179178

180-
expect(toastsShowSpy).toHaveBeenCalledExactlyOnceWith({
179+
expect(infoSignOut).toHaveBeenCalledExactlyOnceWith({
181180
text: expect.stringMatching(/sign-?ups/i),
182-
level: 'info'
183-
});
184-
expect(signOut).toHaveBeenCalledExactlyOnceWith({
185-
resetUrl: true,
186181
source: 'signups-closed'
187182
});
183+
expect(signOut).not.toHaveBeenCalled();
188184
});
189185

190186
it('should load addresses from the backend', async () => {

0 commit comments

Comments
 (0)