@schibsted/account-sdk-browser 4.8.7-beta.8 → 5.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schibsted/account-sdk-browser",
3
- "version": "4.8.7-beta.8",
3
+ "version": "5.0.0-beta.1",
4
4
  "description": "Schibsted account SDK for browsers",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/identity.js CHANGED
@@ -148,6 +148,10 @@ const HAS_SESSION_CACHE_KEY = 'hasSession-cache';
148
148
  const SESSION_CALL_BLOCKED_CACHE_KEY = 'sessionCallBlocked-cache';
149
149
  const SESSION_CALL_BLOCKED_TTL = 1000 * 60 * 5;
150
150
 
151
+ const TAB_ID_KEY = 'tab-id-cache';
152
+ const TAB_ID = Math.floor(Math.random() * 100000)
153
+ const TAB_ID_TTL = 1000 * 60 * 60 * 24 * 30;
154
+
151
155
  const globalWindow = () => window;
152
156
 
153
157
  /**
@@ -185,7 +189,8 @@ export class Identity extends EventEmitter {
185
189
  this._sessionInitiatedSent = false;
186
190
  this.window = window;
187
191
  this.clientId = clientId;
188
- this.cache = new Cache(() => this.window && this.window.sessionStorage);
192
+ this.sessionStorageCache = new Cache(() => this.window && this.window.sessionStorage);
193
+ this.localStorageCache = new Cache(() => this.window && this.window.localStorage);
189
194
  this.redirectUri = redirectUri;
190
195
  this.env = env;
191
196
  this.log = log;
@@ -207,6 +212,23 @@ export class Identity extends EventEmitter {
207
212
  this._unblockSessionCall();
208
213
  }
209
214
 
215
+ /**
216
+ * Read tabId from session storage
217
+ * @returns {number}
218
+ * @private
219
+ */
220
+ _getTabId() {
221
+ if (this._enableSessionCaching) {
222
+ const tabId = this.cache.get(TAB_ID_KEY);
223
+ if (!tabId) {
224
+ this.cache.set(TAB_ID_KEY, TAB_ID, TAB_ID_TTL);
225
+ return TAB_ID;
226
+ }
227
+
228
+ return tabId;
229
+ }
230
+ }
231
+
210
232
  /**
211
233
  * Checks if getting session is blocked
212
234
  * @private
@@ -214,9 +236,7 @@ export class Identity extends EventEmitter {
214
236
  * @returns {boolean|void}
215
237
  */
216
238
  _isSessionCallBlocked(){
217
- if (this._enableSessionCaching) {
218
- return this.cache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
219
- }
239
+ return this.localStorageCache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
220
240
  }
221
241
 
222
242
  /**
@@ -226,15 +246,13 @@ export class Identity extends EventEmitter {
226
246
  * @returns {void}
227
247
  */
228
248
  _blockSessionCall(){
229
- if (this._enableSessionCaching) {
230
- const SESSION_CALL_BLOCKED = true;
249
+ const SESSION_CALL_BLOCKED = true;
231
250
 
232
- this.cache.set(
233
- SESSION_CALL_BLOCKED_CACHE_KEY,
234
- SESSION_CALL_BLOCKED,
235
- SESSION_CALL_BLOCKED_TTL
236
- );
237
- }
251
+ this.localStorageCache.set(
252
+ SESSION_CALL_BLOCKED_CACHE_KEY,
253
+ SESSION_CALL_BLOCKED,
254
+ SESSION_CALL_BLOCKED_TTL
255
+ );
238
256
  }
239
257
 
240
258
  /**
@@ -244,9 +262,7 @@ export class Identity extends EventEmitter {
244
262
  * @returns {void}
245
263
  */
246
264
  _unblockSessionCall(){
247
- if (this._enableSessionCaching) {
248
- this.cache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
249
- }
265
+ this.localStorageCache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
250
266
  }
251
267
 
252
268
  /**
@@ -566,7 +582,7 @@ export class Identity extends EventEmitter {
566
582
  const _getSession = async () => {
567
583
  if (this._enableSessionCaching) {
568
584
  // Try to resolve from cache (it has a TTL)
569
- let cachedSession = this.cache.get(HAS_SESSION_CACHE_KEY);
585
+ let cachedSession = this.sessionStorageCache.get(HAS_SESSION_CACHE_KEY);
570
586
  if (cachedSession) {
571
587
  return _postProcess(cachedSession);
572
588
  }
@@ -577,7 +593,7 @@ export class Identity extends EventEmitter {
577
593
  } catch (err) {
578
594
  if (err && err.code === 400 && this._enableSessionCaching) {
579
595
  const expiresIn = 1000 * (err.expiresIn || 300);
580
- this.cache.set(HAS_SESSION_CACHE_KEY, { error: err }, expiresIn);
596
+ this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, { error: err }, expiresIn);
581
597
  }
582
598
  throw err;
583
599
  }
@@ -589,12 +605,12 @@ export class Identity extends EventEmitter {
589
605
 
590
606
  await this.callbackBeforeRedirect();
591
607
 
592
- return this._sessionService.makeUrl(sessionData.redirectURL);
608
+ return this._sessionService.makeUrl(sessionData.redirectURL, {tabId: this._getTabId()});
593
609
  }
594
610
 
595
611
  if (this._enableSessionCaching) {
596
612
  const expiresIn = 1000 * (sessionData.expiresIn || 300);
597
- this.cache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn);
613
+ this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn);
598
614
  }
599
615
  }
600
616
 
@@ -642,7 +658,7 @@ export class Identity extends EventEmitter {
642
658
  * @returns {void}
643
659
  */
644
660
  clearCachedUserSession() {
645
- this.cache.delete(HAS_SESSION_CACHE_KEY);
661
+ this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
646
662
  }
647
663
 
648
664
  /**
@@ -853,7 +869,7 @@ export class Identity extends EventEmitter {
853
869
  prompt = 'select_account'
854
870
  }) {
855
871
  this._closePopup();
856
- this.cache.delete(HAS_SESSION_CACHE_KEY);
872
+ this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
857
873
  const url = this.loginUrl({
858
874
  state,
859
875
  acrValues,
@@ -902,7 +918,7 @@ export class Identity extends EventEmitter {
902
918
  * @return {void}
903
919
  */
904
920
  logout(redirectUri = this.redirectUri) {
905
- this.cache.delete(HAS_SESSION_CACHE_KEY);
921
+ this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
906
922
  this._maybeClearVarnishCookie();
907
923
  this.emit('logout');
908
924
  this.window.location.href = this.logoutUrl(redirectUri);
package/src/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Automatically generated in 'npm version' by scripts/genversion.js
2
2
 
3
3
  'use strict'
4
- const version = '4.8.7-beta.8';
4
+ const version = '5.0.0-beta.1';
5
5
  export default version;