Skip to content

Commit

Permalink
refactor: consistently use sessionStorage instead of `window.sessio…
Browse files Browse the repository at this point in the history
…nStorage`

* see MDN examples: https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage#basic_usage
* this is supposed to be the same but it is still not SSR safe but so far we do not use it in SSR code
  • Loading branch information
shauke committed Jan 14, 2025
1 parent 02051ca commit fdd3bb0
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
12 changes: 6 additions & 6 deletions src/app/core/identity-provider/auth0.identity-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ export class Auth0IdentityProvider implements IdentityProvider {
),
whenTruthy(),
switchMap(idToken => {
const inviteUserId = window.sessionStorage.getItem('invite-userid');
const inviteHash = window.sessionStorage.getItem('invite-hash');
const inviteUserId = sessionStorage.getItem('invite-userid');
const inviteHash = sessionStorage.getItem('invite-hash');
return inviteUserId && inviteHash
? this.inviteRegistration(idToken, inviteUserId, inviteHash).pipe(
tap(() => {
window.sessionStorage.removeItem('invite-userid');
window.sessionStorage.removeItem('invite-hash');
sessionStorage.removeItem('invite-userid');
sessionStorage.removeItem('invite-hash');
})
)
: this.normalSignInRegistration(idToken);
Expand Down Expand Up @@ -222,8 +222,8 @@ export class Auth0IdentityProvider implements IdentityProvider {

triggerInvite(route: ActivatedRouteSnapshot): TriggerReturnType {
this.router.navigateByUrl('/loading');
window.sessionStorage.setItem('invite-userid', route.queryParams.uid);
window.sessionStorage.setItem('invite-hash', route.queryParams.Hash);
sessionStorage.setItem('invite-userid', route.queryParams.uid);
sessionStorage.setItem('invite-hash', route.queryParams.Hash);
return this.oauthService.loadDiscoveryDocumentAndLogin({
state: route.queryParams.returnUrl,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Co Browse Identity Provider', () => {
resetCalls(accountFacade);
resetCalls(checkoutFacade);

window.sessionStorage.clear();
sessionStorage.clear();
});

describe('init', () => {
Expand All @@ -69,7 +69,7 @@ describe('Co Browse Identity Provider', () => {
it('should add basket-id to session storage, when basket is available', () => {
when(checkoutFacade.basket$).thenReturn(of(BasketMockData.getBasket()));
identityProvider.init();
expect(window.sessionStorage.getItem('basket-id')).toEqual(BasketMockData.getBasket().id);
expect(sessionStorage.getItem('basket-id')).toEqual(BasketMockData.getBasket().id);
});
});

Expand All @@ -82,12 +82,12 @@ describe('Co Browse Identity Provider', () => {
});

it('should remove api token and basket-id on logout', done => {
expect(window.sessionStorage.getItem('basket-id')).toEqual(BasketMockData.getBasket().id);
expect(sessionStorage.getItem('basket-id')).toEqual(BasketMockData.getBasket().id);

const logoutTrigger$ = identityProvider.triggerLogout() as Observable<UrlTree>;

logoutTrigger$.subscribe(() => {
expect(window.sessionStorage.getItem('basket-id')).toBeNull();
expect(sessionStorage.getItem('basket-id')).toBeNull();
verify(accountFacade.logoutUser()).once();
done();
});
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/identity-provider/co-browse.identity-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class CoBrowseIdentityProvider implements IdentityProvider {
this.apiTokenService.restore$(['user', 'order']).subscribe(noop);

this.checkoutFacade.basket$.pipe(whenTruthy(), first()).subscribe(basketView => {
window.sessionStorage.setItem('basket-id', basketView.id);
sessionStorage.setItem('basket-id', basketView.id);
});
}

Expand Down Expand Up @@ -93,7 +93,7 @@ export class CoBrowseIdentityProvider implements IdentityProvider {
}

triggerLogout(): TriggerReturnType {
window.sessionStorage.removeItem('basket-id');
sessionStorage.removeItem('basket-id');
this.accountFacade.logoutUser(); // user will be logged out and related refresh token is revoked on server
return this.accountFacade.isLoggedIn$.pipe(
// wait until the user is logged out before you go to homepage to prevent unnecessary REST calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Icm Identity Provider', () => {
resetCalls(apiTokenService);
resetCalls(accountFacade);

window.sessionStorage.clear();
sessionStorage.clear();
});

describe('init', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/store/customer/basket/basket.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ describe('Basket Effects', () => {

describe('with basket-id in session storage', () => {
beforeEach(() => {
window.sessionStorage.clear();
sessionStorage.clear();
});

it('should map to action of type LoadBasketWithId', () => {
window.sessionStorage.setItem('basket-id', 'BID');
sessionStorage.setItem('basket-id', 'BID');
const action = loadBasket();
const completion = loadBasketWithId({ basketId: 'BID' });
actions$ = hot('-a-a-a', { a: action });
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/store/customer/basket/basket.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ export class BasketEffects {
this.actions$.pipe(
ofType(loadBasket),
mergeMap(() =>
!SSR && window.sessionStorage.getItem('basket-id')
? of(loadBasketWithId({ basketId: window.sessionStorage.getItem('basket-id') }))
!SSR && sessionStorage.getItem('basket-id')
? of(loadBasketWithId({ basketId: sessionStorage.getItem('basket-id') }))
: this.basketService.getBasket().pipe(
map(basket => loadBasketSuccess({ basket })),
mapErrorToAction(loadBasketFail)
Expand Down

0 comments on commit fdd3bb0

Please sign in to comment.