diff --git a/__tests__/identity.js b/__tests__/identity.js index 0ac7f31..4406eaa 100644 --- a/__tests__/identity.js +++ b/__tests__/identity.js @@ -563,7 +563,7 @@ describe('Identity', () => { jest.spyOn(Date, 'now') .mockReturnValue(new Date("2019-11-09T10:00:00").getTime()); - const getExpiresOn = () => JSON.parse(identity.sessionStorageCache.cache.get('hasSession-cache')).expiresOn; + const getExpiresOn = () => JSON.parse(identity.cache.cache.get('hasSession-cache')).expiresOn; await identity.hasSession(); diff --git a/package-lock.json b/package-lock.json index f5bfdf9..8a3d319 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@schibsted/account-sdk-browser", - "version": "5.0.0", + "version": "5.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@schibsted/account-sdk-browser", - "version": "5.0.0", + "version": "5.0.1", "license": "MIT", "dependencies": { "tiny-emitter": "^2.1.0" diff --git a/package.json b/package.json index 01e289e..79b47b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@schibsted/account-sdk-browser", - "version": "5.0.0", + "version": "5.0.1", "description": "Schibsted account SDK for browsers", "main": "index.js", "type": "module", diff --git a/src/identity.d.ts b/src/identity.d.ts index e75406c..49c10d7 100644 --- a/src/identity.d.ts +++ b/src/identity.d.ts @@ -28,8 +28,7 @@ export class Identity extends TinyEmitter { _sessionInitiatedSent: boolean; window: any; clientId: string; - sessionStorageCache: any; - localStorageCache: any; + cache: any; redirectUri: string; env: string; log: Function; diff --git a/src/identity.js b/src/identity.js index f9b53d3..3450eb6 100644 --- a/src/identity.js +++ b/src/identity.js @@ -189,8 +189,7 @@ export class Identity extends EventEmitter { this._sessionInitiatedSent = false; this.window = window; this.clientId = clientId; - this.sessionStorageCache = new Cache(() => this.window && this.window.sessionStorage); - this.localStorageCache = new Cache(() => this.window && this.window.localStorage); + this.cache = new Cache(() => this.window && this.window.sessionStorage); this.redirectUri = redirectUri; this.env = env; this.log = log; @@ -220,9 +219,9 @@ export class Identity extends EventEmitter { */ _getTabId() { if (this._enableSessionCaching) { - const tabId = this.sessionStorageCache.get(TAB_ID_KEY); + const tabId = this.cache.get(TAB_ID_KEY); if (!tabId) { - this.sessionStorageCache.set(TAB_ID_KEY, TAB_ID, TAB_ID_TTL); + this.cache.set(TAB_ID_KEY, TAB_ID, TAB_ID_TTL); return TAB_ID; } @@ -237,7 +236,9 @@ export class Identity extends EventEmitter { * @returns {number/null} */ _isSessionCallBlocked(){ - return this.localStorageCache.get(SESSION_CALL_BLOCKED_CACHE_KEY); + if (this._enableSessionCaching) { + return this.cache.get(SESSION_CALL_BLOCKED_CACHE_KEY); + } } /** @@ -247,13 +248,15 @@ export class Identity extends EventEmitter { * @returns {void} */ _blockSessionCall(){ - const SESSION_CALL_BLOCKED_BY_TAB = this._tabId; + if (this._enableSessionCaching) { + const SESSION_CALL_BLOCKED_BY_TAB = this._tabId; - this.localStorageCache.set( - SESSION_CALL_BLOCKED_CACHE_KEY, + this.cache.set( + SESSION_CALL_BLOCKED_CACHE_KEY, SESSION_CALL_BLOCKED_BY_TAB, - SESSION_CALL_BLOCKED_TTL - ); + SESSION_CALL_BLOCKED_TTL + ); + } } /** @@ -264,7 +267,9 @@ export class Identity extends EventEmitter { */ _unblockSessionCallByTab(){ if (this._isSessionCallBlocked() === this._tabId) { - this.localStorageCache.delete(SESSION_CALL_BLOCKED_CACHE_KEY); + if (this._enableSessionCaching) { + this.cache.delete(SESSION_CALL_BLOCKED_CACHE_KEY); + } } } @@ -585,7 +590,7 @@ export class Identity extends EventEmitter { const _getSession = async () => { if (this._enableSessionCaching) { // Try to resolve from cache (it has a TTL) - let cachedSession = this.sessionStorageCache.get(HAS_SESSION_CACHE_KEY); + let cachedSession = this.cache.get(HAS_SESSION_CACHE_KEY); if (cachedSession) { return _postProcess(cachedSession); } @@ -596,7 +601,7 @@ export class Identity extends EventEmitter { } catch (err) { if (err && err.code === 400 && this._enableSessionCaching) { const expiresIn = 1000 * (err.expiresIn || 300); - this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, { error: err }, expiresIn); + this.cache.set(HAS_SESSION_CACHE_KEY, { error: err }, expiresIn); } throw err; } @@ -613,7 +618,7 @@ export class Identity extends EventEmitter { if (this._enableSessionCaching) { const expiresIn = 1000 * (sessionData.expiresIn || 300); - this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn); + this.cache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn); } } @@ -661,7 +666,7 @@ export class Identity extends EventEmitter { * @returns {void} */ clearCachedUserSession() { - this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY); + this.cache.delete(HAS_SESSION_CACHE_KEY); } /** @@ -872,7 +877,7 @@ export class Identity extends EventEmitter { prompt = 'select_account' }) { this._closePopup(); - this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY); + this.cache.delete(HAS_SESSION_CACHE_KEY); const url = this.loginUrl({ state, acrValues, @@ -921,7 +926,7 @@ export class Identity extends EventEmitter { * @return {void} */ logout(redirectUri = this.redirectUri) { - this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY); + this.cache.delete(HAS_SESSION_CACHE_KEY); this._maybeClearVarnishCookie(); this.emit('logout'); this.window.location.href = this.logoutUrl(redirectUri); diff --git a/src/version.js b/src/version.js index e818658..11862d5 100644 --- a/src/version.js +++ b/src/version.js @@ -1,5 +1,5 @@ // Automatically generated in 'npm version' by scripts/genversion.js 'use strict' -const version = '5.0.0'; +const version = '5.0.1'; export default version;