Skip to content

Commit

Permalink
fix: remove token informations from local storage after succesful log…
Browse files Browse the repository at this point in the history
…out (#1488)

* remove after succesful logout the related token information from the localStorage
* adapt e2e tests
  • Loading branch information
Eisie96 authored Aug 23, 2023
1 parent 58702f1 commit 1c0bf6e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
21 changes: 21 additions & 0 deletions e2e/cypress/e2e/specs/account/login-user.b2c.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,33 @@ describe('Returning User', () => {
});
});

it('should have saved apiToken as cookie and within localStorage', () => {
cy.getCookie('apiToken').then(cookie => {
expect(cookie).to.not.be.empty;
cy.wrap(JSON.parse(decodeURIComponent(cookie.value))).should('have.property', 'type', 'user');
});

cy.getAllLocalStorage().then(
localStorage => expect(localStorage[Cypress.config('baseUrl')].access_token).to.not.be.empty
);
});

it('should logout and be redirected to home page', () => {
at(MyAccountPage, page => {
page.header.logout();
});
at(HomePage);
});

it('should have removed apiToken cookie and infos from localStorage', () => {
cy.getCookie('apiToken').then(cookie => {
expect(cookie).to.be.null;
});

cy.getAllLocalStorage().then(
localStorage => expect(localStorage[Cypress.config('baseUrl')].access_token).to.be.undefined
);
});
});

describe('with wrong password', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/app/core/identity-provider/icm.identity-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Observable, Subject, of } from 'rxjs';
import { anything, instance, mock, resetCalls, verify, when } from 'ts-mockito';

import { AccountFacade } from 'ish-core/facades/account.facade';
import { TokenService } from 'ish-core/services/token/token.service';
import { selectQueryParam } from 'ish-core/store/core/router';
import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service';

Expand All @@ -23,6 +24,7 @@ describe('Icm Identity Provider', () => {
providers: [
{ provide: AccountFacade, useFactory: () => instance(accountFacade) },
{ provide: ApiTokenService, useFactory: () => instance(apiTokenService) },
{ provide: TokenService, useFactory: () => instance(mock(TokenService)) },
provideMockStore(),
],
}).compileComponents();
Expand Down
8 changes: 6 additions & 2 deletions src/app/core/identity-provider/icm.identity-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Router } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { Observable, noop } from 'rxjs';
import { filter, map, switchMap, take } from 'rxjs/operators';
import { filter, map, switchMap, take, tap } from 'rxjs/operators';

import { AccountFacade } from 'ish-core/facades/account.facade';
import { TokenService } from 'ish-core/services/token/token.service';
import { selectQueryParam } from 'ish-core/store/core/router';
import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service';

Expand All @@ -17,7 +18,8 @@ export class ICMIdentityProvider implements IdentityProvider {
private router: Router,
private store: Store,
private apiTokenService: ApiTokenService,
private accountFacade: AccountFacade
private accountFacade: AccountFacade,
private tokenService: TokenService
) {}

getCapabilities() {
Expand Down Expand Up @@ -46,11 +48,13 @@ export class ICMIdentityProvider implements IdentityProvider {
}

triggerLogout(): TriggerReturnType {
// revoke current token
this.accountFacade.logoutUser();
return this.accountFacade.isLoggedIn$.pipe(
// wait until the user is logged out before you go to homepage to prevent unnecessary REST calls
filter(loggedIn => !loggedIn),
take(1),
tap(() => this.tokenService.logOut()), // remove token from storage when user is logged out
switchMap(() =>
this.store.pipe(
select(selectQueryParam('returnUrl')),
Expand Down
4 changes: 4 additions & 0 deletions src/app/core/services/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export class TokenService {
);
}

logOut() {
this.oAuthService.logOut(true);
}

/**
* Refresh existing tokens, when token is about to expire
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { anyString, anything, instance, mock, resetCalls, verify, when } from 't
import { AccountFacade } from 'ish-core/facades/account.facade';
import { AppFacade } from 'ish-core/facades/app.facade';
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { TokenService } from 'ish-core/services/token/token.service';
import { selectQueryParam } from 'ish-core/store/core/router';
import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service';
import { CookiesService } from 'ish-core/utils/cookies/cookies.service';
Expand Down Expand Up @@ -46,6 +47,7 @@ describe('Punchout Identity Provider', () => {
{ provide: CheckoutFacade, useFactory: () => instance(checkoutFacade) },
{ provide: CookiesService, useFactory: () => instance(cookiesService) },
{ provide: PunchoutService, useFactory: () => instance(punchoutService) },
{ provide: TokenService, useFactory: () => instance(mock(TokenService)) },
provideMockStore(),
],
}).compileComponents();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AccountFacade } from 'ish-core/facades/account.facade';
import { AppFacade } from 'ish-core/facades/app.facade';
import { CheckoutFacade } from 'ish-core/facades/checkout.facade';
import { IdentityProvider, TriggerReturnType } from 'ish-core/identity-provider/identity-provider.interface';
import { TokenService } from 'ish-core/services/token/token.service';
import { selectQueryParam } from 'ish-core/store/core/router';
import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service';
import { CookiesService } from 'ish-core/utils/cookies/cookies.service';
Expand All @@ -26,7 +27,8 @@ export class PunchoutIdentityProvider implements IdentityProvider {
private accountFacade: AccountFacade,
private punchoutService: PunchoutService,
private cookiesService: CookiesService,
private checkoutFacade: CheckoutFacade
private checkoutFacade: CheckoutFacade,
private tokenService: TokenService
) {}

getCapabilities() {
Expand Down Expand Up @@ -132,6 +134,7 @@ export class PunchoutIdentityProvider implements IdentityProvider {
// wait until the user is logged out before you go to homepage to prevent unnecessary REST calls
filter(loggedIn => !loggedIn),
take(1),
tap(() => this.tokenService.logOut()), // remove token from storage when user is logged out
switchMap(() =>
this.store.pipe(
select(selectQueryParam('returnUrl')),
Expand Down

0 comments on commit 1c0bf6e

Please sign in to comment.