Skip to content

Commit

Permalink
refactor: adapt 'apiToken' cookie handling after ICM api token changes (
Browse files Browse the repository at this point in the history
#1105)

BREAKING CHANGES: For a working hybrid approach ICM 7.10.32.16-LTS or 7.10.38.6-LTS or newer is required. The changes in the PWA should not be incompatible with older ICM version if no hybrid approach is needed.
  • Loading branch information
shauke committed Apr 29, 2022
1 parent 1bed06a commit 7e24ba1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/app/core/identity-provider/auth0.identity-provider.ts
Expand Up @@ -80,7 +80,7 @@ export class Auth0IdentityProvider implements IdentityProvider {
});
this.oauthService.setupAutomaticSilentRefresh();
this.apiTokenService
.restore$(['basket', 'order'])
.restore$(['user', 'order'])
.pipe(
switchMap(() => from(this.oauthService.loadDiscoveryDocumentAndTryLogin())),
switchMap(() =>
Expand Down
48 changes: 26 additions & 22 deletions src/app/core/utils/api-token/api-token.service.ts
Expand Up @@ -28,12 +28,14 @@ import { getLoggedInUser, getUserAuthorized, loadUserByAPIToken } from 'ish-core
import { CookiesService } from 'ish-core/utils/cookies/cookies.service';
import { mapToProperty, whenTruthy } from 'ish-core/utils/operators';

type ApiTokenCookieType = 'user' | 'basket' | 'order';
export type ApiTokenCookieType = 'user' | 'order';

interface ApiTokenCookie {
apiToken: string;
type: ApiTokenCookieType;
isAnonymous?: boolean;
orderId?: string;
creator?: string;
}

@Injectable({ providedIn: 'root' })
Expand Down Expand Up @@ -67,11 +69,11 @@ export class ApiTokenService {
.pipe(
map(([user, basket, orderId, apiToken]): ApiTokenCookie => {
if (user) {
return { type: 'user', apiToken };
return { apiToken, type: 'user', isAnonymous: false, creator: 'pwa' };
} else if (basket) {
return { type: 'basket', apiToken };
return { apiToken, type: 'user', isAnonymous: true, creator: 'pwa' };
} else if (orderId) {
return { type: 'order', apiToken, orderId };
return { apiToken, type: 'order', orderId, creator: 'pwa' };
} else {
const apiTokenCookieString = this.cookiesService.get('apiToken');
const apiTokenCookie: ApiTokenCookie = apiTokenCookieString
Expand Down Expand Up @@ -139,10 +141,10 @@ export class ApiTokenService {

hasUserApiTokenCookie() {
const apiTokenCookie = this.parseCookie();
return apiTokenCookie?.type === 'user';
return apiTokenCookie?.type === 'user' && !apiTokenCookie?.isAnonymous;
}

restore$(types: ApiTokenCookieType[] = ['user', 'basket', 'order']): Observable<boolean> {
restore$(types: ApiTokenCookieType[] = ['user', 'order']): Observable<boolean> {
if (isPlatformServer(this.platformId)) {
return of(true);
}
Expand All @@ -153,23 +155,25 @@ export class ApiTokenService {
if (types.includes(cookie?.type)) {
switch (cookie?.type) {
case 'user': {
this.store.dispatch(loadUserByAPIToken());
return race(
this.store.pipe(select(getUserAuthorized), whenTruthy(), take(1)),
timer(5000).pipe(map(() => false))
);
if (cookie.isAnonymous) {
this.store.dispatch(loadBasketByAPIToken({ apiToken: cookie.apiToken }));
return race(
this.store.pipe(
select(getCurrentBasketId),
whenTruthy(),
take(1),
map(() => true)
),
timer(5000).pipe(map(() => false))
);
} else {
this.store.dispatch(loadUserByAPIToken());
return race(
this.store.pipe(select(getUserAuthorized), whenTruthy(), take(1)),
timer(5000).pipe(map(() => false))
);
}
}
case 'basket':
this.store.dispatch(loadBasketByAPIToken({ apiToken: cookie.apiToken }));
return race(
this.store.pipe(
select(getCurrentBasketId),
whenTruthy(),
take(1),
map(() => true)
),
timer(5000).pipe(map(() => false))
);
case 'order': {
this.store.dispatch(loadOrderByAPIToken({ orderId: cookie.orderId, apiToken: cookie.apiToken }));
return race(
Expand Down
Expand Up @@ -10,7 +10,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 { selectQueryParam } from 'ish-core/store/core/router';
import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service';
import { ApiTokenCookieType, ApiTokenService } from 'ish-core/utils/api-token/api-token.service';
import { CookiesService } from 'ish-core/utils/cookies/cookies.service';
import { makeHttpError } from 'ish-core/utils/dev/api-service-utils';
import { BasketMockData } from 'ish-core/utils/dev/basket-mock-data';
Expand All @@ -20,8 +20,6 @@ import { PunchoutService } from '../services/punchout/punchout.service';

import { PunchoutIdentityProvider } from './punchout-identity-provider';

type ApiTokenCookieType = 'user' | 'basket' | 'order';

function getSnapshot(queryParams: Params): ActivatedRouteSnapshot {
return {
queryParamMap: convertToParamMap(queryParams),
Expand Down

0 comments on commit 7e24ba1

Please sign in to comment.