Skip to content

Commit

Permalink
fix: resolve review comments (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
hunger-programmer authored and bogdan-niculescu-sch committed Apr 10, 2024
1 parent c47f4b2 commit 1deedd6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
60 changes: 51 additions & 9 deletions src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 * 5;

const globalWindow = () => window;

/**
Expand Down Expand Up @@ -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 = {};
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -491,9 +537,8 @@ export class Identity extends EventEmitter {
* @return {Promise<HasSessionSuccessResponse|HasSessionFailureResponse>}
*/
hasSession() {
const checkIfSessionOngoing = this.cache.get("sessionFlowOngoing");
if (checkIfSessionOngoing)
{
const isSessionCallBlocked = this._isSessionCallBlocked()
if (isSessionCallBlocked) {
return this._session;
}

Expand Down Expand Up @@ -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();

Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/version.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 1deedd6

Please sign in to comment.