Skip to content

Commit

Permalink
Merge branch 'master' into limit-clear-local-storage-clear
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan-niculescu-sch committed Apr 17, 2024
2 parents ff16970 + 9f2e548 commit ecf4a73
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion __tests__/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
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": "5.0.0",
"version": "5.0.1",
"description": "Schibsted account SDK for browsers",
"main": "index.js",
"type": "module",
Expand Down
3 changes: 1 addition & 2 deletions src/identity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 22 additions & 17 deletions src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

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

/**
Expand All @@ -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,

Check warning on line 256 in src/identity.js

View workflow job for this annotation

GitHub Actions / Run js tests

Expected indentation of 16 spaces but found 12
SESSION_CALL_BLOCKED_TTL
);
SESSION_CALL_BLOCKED_TTL
);
}
}

/**
Expand All @@ -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);

Check warning on line 271 in src/identity.js

View workflow job for this annotation

GitHub Actions / Run js tests

Expected indentation of 16 spaces but found 12
}

Check warning on line 272 in src/identity.js

View workflow job for this annotation

GitHub Actions / Run js tests

Expected indentation of 12 spaces but found 8
}
}

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

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

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
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 = '5.0.0';
const version = '5.0.1';
export default version;

0 comments on commit ecf4a73

Please sign in to comment.