Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move session call lock to local storage #275

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 16 additions & 21 deletions src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,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 @@ -214,9 +215,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 @@ -226,15 +225,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 @@ -244,9 +241,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 @@ -566,7 +561,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 @@ -577,7 +572,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 @@ -594,7 +589,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 @@ -642,7 +637,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 @@ -853,7 +848,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 @@ -902,7 +897,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
Loading