diff --git a/stores/__tests__/auth-store-redirect-after-login.test.ts b/stores/__tests__/auth-store-redirect-after-login.test.ts new file mode 100644 index 000000000..e55c00eb7 --- /dev/null +++ b/stores/__tests__/auth-store-redirect-after-login.test.ts @@ -0,0 +1,29 @@ +import { beforeEach, describe, expect, it } from 'vitest'; +import { saveRedirectAfterLogin } from '../auth-store'; + +// Where login sends the user back to. The app's home is where a login lands +// anyway; saving it made the login page navigate a second time, to /en/en +// (the locale-aware router prefixes the locale again), racing the first. + +describe('saveRedirectAfterLogin', () => { + beforeEach(() => sessionStorage.clear()); + + it('saves nothing on the home page, and drops an older target', () => { + sessionStorage.setItem('redirect_after_login', '/en/settings'); + window.history.pushState({}, '', '/en'); + saveRedirectAfterLogin(); + expect(sessionStorage.getItem('redirect_after_login')).toBeNull(); + }); + + it('keeps a deep link, query and hash included', () => { + window.history.pushState({}, '', '/en/calendar?view=day#e1'); + saveRedirectAfterLogin(); + expect(sessionStorage.getItem('redirect_after_login')).toBe('/en/calendar?view=day#e1'); + }); + + it('keeps the home page with a query, which carries a deep link', () => { + window.history.pushState({}, '', '/en?email=abc'); + saveRedirectAfterLogin(); + expect(sessionStorage.getItem('redirect_after_login')).toBe('/en?email=abc'); + }); +}); diff --git a/stores/auth-store.ts b/stores/auth-store.ts index 9853ad53b..f448bcb70 100644 --- a/stores/auth-store.ts +++ b/stores/auth-store.ts @@ -856,6 +856,15 @@ export function saveRedirectAfterLogin(): void { const loginPath = getLocaleLoginPath(); const currentPath = `${window.location.pathname}${window.location.search}${window.location.hash}`; + // The app's home is where a login lands anyway. Saving it only made the + // login page navigate a second time, through the locale-aware router, + // which prefixes the locale again (/en/en) and races the first. + const home = `${getPathPrefix()}/${getLocaleFromPath()}`; + if (currentPath === home || currentPath === `${home}/`) { + sessionStorage.removeItem('redirect_after_login'); + return; + } + if (currentPath !== loginPath) { sessionStorage.setItem('redirect_after_login', currentPath); }