Skip to content

Commit 6dc690f

Browse files
authored
feat(account-center): restore route after sign-in redirects (#7985)
1 parent 4e573fb commit 6dc690f

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

packages/account-center/src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import BrandingHeader from './components/BrandingHeader';
1212
import ErrorPage from './components/ErrorPage';
1313
import initI18n from './i18n/init';
1414
import Home from './pages/Home';
15+
import { handleAccountCenterRoute } from './utils/account-center-route';
1516

1617
import '@experience/shared/scss/normalized.scss';
1718

1819
void initI18n();
20+
handleAccountCenterRoute();
1921

2022
const redirectUri = `${window.location.origin}/account-center`;
2123

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const accountCenterBasePath = '/account-center';
2+
const storageKey = 'account-center-route-cache';
3+
4+
const knownRoutePrefixes: readonly string[] = ['/email', '/phone', '/username', '/password'];
5+
6+
const isKnownRoute = (pathname?: string): pathname is string =>
7+
pathname !== undefined &&
8+
knownRoutePrefixes.some((prefix) =>
9+
pathname.replace(accountCenterBasePath, '').startsWith(prefix)
10+
);
11+
12+
const parseStoredRoute = (storedRoute: string | undefined): string | undefined => {
13+
if (storedRoute && isKnownRoute(storedRoute)) {
14+
return storedRoute;
15+
}
16+
return undefined;
17+
};
18+
19+
const shouldSkipHandling = (search: string) => {
20+
const parameters = new URLSearchParams(search);
21+
return parameters.has('code') || parameters.has('error');
22+
};
23+
24+
/**
25+
* Handle Account Center route restoration for sign in redirect.
26+
*/
27+
export const handleAccountCenterRoute = () => {
28+
if (shouldSkipHandling(window.location.search)) {
29+
return;
30+
}
31+
32+
// Restore the stored route if the current path is the base path.
33+
if (window.location.pathname === accountCenterBasePath) {
34+
const storedRoute = parseStoredRoute(sessionStorage.getItem(storageKey) ?? undefined);
35+
if (!storedRoute) {
36+
sessionStorage.removeItem(storageKey);
37+
return;
38+
}
39+
40+
const { search, hash } = window.location;
41+
window.history.replaceState({}, '', `${storedRoute}${search}${hash}`);
42+
} else if (isKnownRoute(window.location.pathname)) {
43+
sessionStorage.setItem(storageKey, window.location.pathname);
44+
}
45+
};

0 commit comments

Comments
 (0)