Skip to content

Commit

Permalink
addressing code review
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleysmithTTD committed Sep 3, 2024
1 parent 25b73d0 commit a4ea126
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
5 changes: 1 addition & 4 deletions src/cookieManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ export class CookieManager {
(previousOptions.cookieDomain ?? '') +
';expires=Tue, 1 Jan 1980 23:59:59 GMT';
}
private getCookie() {
return getCookie(this._cookieName);
}

private migrateLegacyCookie(identity: LegacySDKCookie, now: number): Identity {
const newCookie = enrichIdentity(identity, now);
Expand All @@ -95,7 +92,7 @@ export class CookieManager {
}

public loadIdentityFromCookie(): Identity | OptoutIdentity | null {
const payload = this.getCookie();
const payload = getCookie(this._cookieName);
if (payload) {
const result = JSON.parse(payload) as unknown;
if (isValidIdentity(result)) return result;
Expand Down
2 changes: 1 addition & 1 deletion src/integrationTests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ describe('SDK bootstraps itself if init has already been completed', () => {
});
});

describe('Public functions working without init', () => {
describe('Token retrieval and related public functions working without init', () => {
const makeIdentity = mocks.makeIdentityV2;
const email = 'test@test.com';
const emailHash = 'lz3+Rj7IV4X1+Vr1ujkG7tstkxwk5pgkqJ6mXbpOgTs=';
Expand Down
4 changes: 2 additions & 2 deletions src/localStorageManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isOptoutIdentity, isValidIdentity, OptoutIdentity, Identity } from './Identity';

export function loadIdentityFromLocalStorage(storageKey: string): Identity | OptoutIdentity | null {
export function loadIdentityWithStorageKey(storageKey: string): Identity | OptoutIdentity | null {
const payload = getValue(storageKey);
if (payload) {
const result = JSON.parse(payload) as unknown;
Expand Down Expand Up @@ -31,6 +31,6 @@ export class LocalStorageManager {
}

public loadIdentityFromLocalStorage(): Identity | OptoutIdentity | null {
return loadIdentityFromLocalStorage(this._storageKey);
return loadIdentityWithStorageKey(this._storageKey);
}
}
5 changes: 3 additions & 2 deletions src/sdkBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { hashAndEncodeIdentifier } from './encoding/hash';
import { ProductDetails, ProductName } from './product';
import { storeConfig, updateConfig } from './configManager';
import { loadIdentityFromCookieNoLegacy } from './cookieManager';
import { loadIdentityFromLocalStorage } from './localStorageManager';
import { loadIdentityWithStorageKey } from './localStorageManager';

function hasExpired(expiry: number, now = Date.now()) {
return expiry <= now;
Expand Down Expand Up @@ -266,6 +266,7 @@ export abstract class SdkBase {

private temporarilyUnavailable(identity: Identity | OptoutIdentity | null | undefined) {
if (!identity && this._apiClient?.hasActiveRequests()) return true;
// returns true if identity is expired but refreshable
if (identity && hasExpired(identity.identity_expires) && !hasExpired(identity.refresh_expires))
return true;
return false;
Expand Down Expand Up @@ -460,7 +461,7 @@ export abstract class SdkBase {
private getIdentityNoInit() {
return (
loadIdentityFromCookieNoLegacy(this._product.cookieName) ??
loadIdentityFromLocalStorage(this._product.localStorageKey)
loadIdentityWithStorageKey(this._product.localStorageKey)
);
}

Expand Down

0 comments on commit a4ea126

Please sign in to comment.