diff --git a/package-lock.json b/package-lock.json index a68efb2..1223598 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@schibsted/account-sdk-browser", - "version": "4.8.7-beta.7", + "version": "4.8.7-beta.8", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@schibsted/account-sdk-browser", - "version": "4.8.7-beta.7", + "version": "4.8.7-beta.8", "license": "MIT", "dependencies": { "tiny-emitter": "^2.1.0" diff --git a/package.json b/package.json index e1d58b2..400fdf3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@schibsted/account-sdk-browser", - "version": "4.8.7-beta.7", + "version": "4.8.7-beta.8", "description": "Schibsted account SDK for browsers", "main": "index.js", "type": "module", diff --git a/src/identity.js b/src/identity.js index 4bbc302..7d530f9 100644 --- a/src/identity.js +++ b/src/identity.js @@ -145,6 +145,9 @@ import version from './version.js'; */ const HAS_SESSION_CACHE_KEY = 'hasSession-cache'; +const SESSION_CALL_BLOCKED_CACHE_KEY = 'sessionCallBlocked-cache'; +const SESSION_CALL_BLOCKED_TTL = 1000 * 60 * 60 * 24 * 30; + const globalWindow = () => window; /** @@ -191,7 +194,6 @@ export class Identity extends EventEmitter { // Internal hack: set to false to always refresh from hassession this._enableSessionCaching = true; - this.cache.delete("sessionFlowOngoing"); // Old session this._session = {}; @@ -201,6 +203,50 @@ export class Identity extends EventEmitter { this._setBffServerUrl(env); this._setOauthServerUrl(env); this._setGlobalSessionServiceUrl(env); + + this._unblockSessionCall(); + } + + /** + * Checks if getting session is blocked + * @private + * + * @returns {boolean|void} + */ + _isSessionCallBlocked(){ + if (this._enableSessionCaching) { + return this.cache.get(SESSION_CALL_BLOCKED_CACHE_KEY); + } + } + + /** + * Block calls to get session + * @private + * + * @returns {void} + */ + _blockSessionCall(){ + if (this._enableSessionCaching) { + const SESSION_CALL_BLOCKED = true; + + this.cache.set( + SESSION_CALL_BLOCKED_CACHE_KEY, + SESSION_CALL_BLOCKED, + SESSION_CALL_BLOCKED_TTL + ); + } + } + + /** + * Unblocks calls to get session + * @private + * + * @returns {void} + */ + _unblockSessionCall(){ + if (this._enableSessionCaching) { + this.cache.delete(SESSION_CALL_BLOCKED_CACHE_KEY); + } } /** @@ -491,9 +537,8 @@ export class Identity extends EventEmitter { * @return {Promise} */ hasSession() { - const checkIfSessionOngoing = this.cache.get("sessionFlowOngoing"); - if (checkIfSessionOngoing) - { + const isSessionCallBlocked = this._isSessionCallBlocked() + if (isSessionCallBlocked) { return this._session; } @@ -540,10 +585,7 @@ export class Identity extends EventEmitter { if (sessionData){ // for expiring session and safari browser do full page redirect to gain new session if(_checkRedirectionNeed(sessionData)){ - if (this._enableSessionCaching) { - const expiresIn = 1000 * (sessionData.expiresIn || 300); - this.cache.set("sessionFlowOngoing", true, expiresIn); - } + this._blockSessionCall(); await this.callbackBeforeRedirect(); @@ -563,7 +605,7 @@ export class Identity extends EventEmitter { sessionData => { this._hasSessionInProgress = false; - if (typeof sessionData === 'string' && isUrl(sessionData)) { + if (isUrl(sessionData)) { return this.window.location.href = sessionData; } diff --git a/src/version.js b/src/version.js index 6d84dcb..be7edf6 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 = '4.8.7-beta.7'; +const version = '4.8.7-beta.8'; export default version;