Skip to content

Commit

Permalink
Add back local storage changes (#282)
Browse files Browse the repository at this point in the history
* Reapply "move session call lock to local storage"

This reverts commit 008e1f9.

* Reapply "fix: name change for cache"

This reverts commit 4a661b7.
  • Loading branch information
bogdan-niculescu-sch authored Apr 17, 2024
1 parent 9f2e548 commit b84823b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 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.cache.cache.get('hasSession-cache')).expiresOn;
const getExpiresOn = () => JSON.parse(identity.sessionStorageCache.cache.get('hasSession-cache')).expiresOn;

await identity.hasSession();

Expand Down
3 changes: 2 additions & 1 deletion src/identity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class Identity extends TinyEmitter {
_sessionInitiatedSent: boolean;
window: any;
clientId: string;
cache: any;
sessionStorageCache: any;
localStorageCache: any;
redirectUri: string;
env: string;
log: Function;
Expand Down
41 changes: 18 additions & 23 deletions src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ export class Identity extends EventEmitter {
this._sessionInitiatedSent = false;
this.window = window;
this.clientId = clientId;
this.cache = new Cache(() => this.window && this.window.sessionStorage);
this.sessionStorageCache = new Cache(() => this.window && this.window.sessionStorage);
this.localStorageCache = new Cache(() => this.window && this.window.localStorage);
this.redirectUri = redirectUri;
this.env = env;
this.log = log;
Expand Down Expand Up @@ -218,9 +219,9 @@ export class Identity extends EventEmitter {
*/
_getTabId() {
if (this._enableSessionCaching) {
const tabId = this.cache.get(TAB_ID_KEY);
const tabId = this.sessionStorageCache.get(TAB_ID_KEY);
if (!tabId) {
this.cache.set(TAB_ID_KEY, TAB_ID, TAB_ID_TTL);
this.sessionStorageCache.set(TAB_ID_KEY, TAB_ID, TAB_ID_TTL);
return TAB_ID;
}

Expand All @@ -235,9 +236,7 @@ export class Identity extends EventEmitter {
* @returns {boolean|void}
*/
_isSessionCallBlocked(){
if (this._enableSessionCaching) {
return this.cache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
}
return this.localStorageCache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
}

/**
Expand All @@ -247,15 +246,13 @@ export class Identity extends EventEmitter {
* @returns {void}
*/
_blockSessionCall(){
if (this._enableSessionCaching) {
const SESSION_CALL_BLOCKED = true;
const SESSION_CALL_BLOCKED = true;

this.cache.set(
SESSION_CALL_BLOCKED_CACHE_KEY,
SESSION_CALL_BLOCKED,
SESSION_CALL_BLOCKED_TTL
);
}
this.localStorageCache.set(
SESSION_CALL_BLOCKED_CACHE_KEY,
SESSION_CALL_BLOCKED,
SESSION_CALL_BLOCKED_TTL
);
}

/**
Expand All @@ -265,9 +262,7 @@ export class Identity extends EventEmitter {
* @returns {void}
*/
_unblockSessionCall(){
if (this._enableSessionCaching) {
this.cache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
}
this.localStorageCache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
}

/**
Expand Down Expand Up @@ -587,7 +582,7 @@ export class Identity extends EventEmitter {
const _getSession = async () => {
if (this._enableSessionCaching) {
// Try to resolve from cache (it has a TTL)
let cachedSession = this.cache.get(HAS_SESSION_CACHE_KEY);
let cachedSession = this.sessionStorageCache.get(HAS_SESSION_CACHE_KEY);
if (cachedSession) {
return _postProcess(cachedSession);
}
Expand All @@ -598,7 +593,7 @@ export class Identity extends EventEmitter {
} catch (err) {
if (err && err.code === 400 && this._enableSessionCaching) {
const expiresIn = 1000 * (err.expiresIn || 300);
this.cache.set(HAS_SESSION_CACHE_KEY, { error: err }, expiresIn);
this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, { error: err }, expiresIn);
}
throw err;
}
Expand All @@ -615,7 +610,7 @@ export class Identity extends EventEmitter {

if (this._enableSessionCaching) {
const expiresIn = 1000 * (sessionData.expiresIn || 300);
this.cache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn);
this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn);
}
}

Expand Down Expand Up @@ -663,7 +658,7 @@ export class Identity extends EventEmitter {
* @returns {void}
*/
clearCachedUserSession() {
this.cache.delete(HAS_SESSION_CACHE_KEY);
this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
}

/**
Expand Down Expand Up @@ -874,7 +869,7 @@ export class Identity extends EventEmitter {
prompt = 'select_account'
}) {
this._closePopup();
this.cache.delete(HAS_SESSION_CACHE_KEY);
this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
const url = this.loginUrl({
state,
acrValues,
Expand Down Expand Up @@ -923,7 +918,7 @@ export class Identity extends EventEmitter {
* @return {void}
*/
logout(redirectUri = this.redirectUri) {
this.cache.delete(HAS_SESSION_CACHE_KEY);
this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
this._maybeClearVarnishCookie();
this.emit('logout');
this.window.location.href = this.logoutUrl(redirectUri);
Expand Down

0 comments on commit b84823b

Please sign in to comment.