@schibsted/account-sdk-browser 4.8.7 → 5.0.0

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",
3
+ "version": "5.0.0",
4
4
  "description": "Schibsted account SDK for browsers",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/identity.d.ts CHANGED
@@ -13,15 +13,17 @@ export class Identity extends TinyEmitter {
13
13
  * @param {function} [options.log] - A function that receives debug log information. If not set,
14
14
  * no logging will be done
15
15
  * @param {object} [options.window] - window object
16
+ * @param {function} [options.callbackBeforeRedirect] - callback triggered before session refresh redirect happen
16
17
  * @throws {SDKError} - If any of options are invalid
17
18
  */
18
- constructor({ clientId, redirectUri, sessionDomain, env, log, window }: {
19
+ constructor({ clientId, redirectUri, sessionDomain, env, log, window, callbackBeforeRedirect }: {
19
20
  clientId: string;
20
21
  sessionDomain: string;
21
22
  redirectUri: string;
22
23
  env?: string;
23
24
  log?: Function;
24
25
  window?: any;
26
+ callbackBeforeRedirect?: Function;
25
27
  });
26
28
  _sessionInitiatedSent: boolean;
27
29
  window: any;
@@ -30,6 +32,7 @@ export class Identity extends TinyEmitter {
30
32
  redirectUri: string;
31
33
  env: string;
32
34
  log: Function;
35
+ callbackBeforeRedirect: Function;
33
36
  _sessionDomain: string;
34
37
  _enableSessionCaching: boolean;
35
38
  _session: {};
package/src/identity.js CHANGED
@@ -145,6 +145,9 @@ import version from './version.js';
145
145
  */
146
146
 
147
147
  const HAS_SESSION_CACHE_KEY = 'hasSession-cache';
148
+ const SESSION_CALL_BLOCKED_CACHE_KEY = 'sessionCallBlocked-cache';
149
+ const SESSION_CALL_BLOCKED_TTL = 1000 * 60 * 5;
150
+
148
151
  const globalWindow = () => window;
149
152
 
150
153
  /**
@@ -160,9 +163,18 @@ export class Identity extends EventEmitter {
160
163
  * @param {function} [options.log] - A function that receives debug log information. If not set,
161
164
  * no logging will be done
162
165
  * @param {object} [options.window] - window object
166
+ * @param {function} [options.callbackBeforeRedirect] - callback triggered before session refresh redirect happen
163
167
  * @throws {SDKError} - If any of options are invalid
164
168
  */
165
- constructor({ clientId, redirectUri, sessionDomain, env = 'PRE', log, window = globalWindow() }) {
169
+ constructor({
170
+ clientId,
171
+ redirectUri,
172
+ sessionDomain,
173
+ env = 'PRE',
174
+ log,
175
+ window = globalWindow(),
176
+ callbackBeforeRedirect = ()=>{}
177
+ }) {
166
178
  super();
167
179
  assert(isNonEmptyString(clientId), 'clientId parameter is required');
168
180
  assert(isObject(window), 'The reference to window is missing');
@@ -177,6 +189,7 @@ export class Identity extends EventEmitter {
177
189
  this.redirectUri = redirectUri;
178
190
  this.env = env;
179
191
  this.log = log;
192
+ this.callbackBeforeRedirect = callbackBeforeRedirect;
180
193
  this._sessionDomain = sessionDomain;
181
194
 
182
195
  // Internal hack: set to false to always refresh from hassession
@@ -190,6 +203,50 @@ export class Identity extends EventEmitter {
190
203
  this._setBffServerUrl(env);
191
204
  this._setOauthServerUrl(env);
192
205
  this._setGlobalSessionServiceUrl(env);
206
+
207
+ this._unblockSessionCall();
208
+ }
209
+
210
+ /**
211
+ * Checks if getting session is blocked
212
+ * @private
213
+ *
214
+ * @returns {boolean|void}
215
+ */
216
+ _isSessionCallBlocked(){
217
+ if (this._enableSessionCaching) {
218
+ return this.cache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
219
+ }
220
+ }
221
+
222
+ /**
223
+ * Block calls to get session
224
+ * @private
225
+ *
226
+ * @returns {void}
227
+ */
228
+ _blockSessionCall(){
229
+ if (this._enableSessionCaching) {
230
+ const SESSION_CALL_BLOCKED = true;
231
+
232
+ this.cache.set(
233
+ SESSION_CALL_BLOCKED_CACHE_KEY,
234
+ SESSION_CALL_BLOCKED,
235
+ SESSION_CALL_BLOCKED_TTL
236
+ );
237
+ }
238
+ }
239
+
240
+ /**
241
+ * Unblocks calls to get session
242
+ * @private
243
+ *
244
+ * @returns {void}
245
+ */
246
+ _unblockSessionCall(){
247
+ if (this._enableSessionCaching) {
248
+ this.cache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
249
+ }
193
250
  }
194
251
 
195
252
  /**
@@ -480,9 +537,15 @@ export class Identity extends EventEmitter {
480
537
  * @return {Promise<HasSessionSuccessResponse|HasSessionFailureResponse>}
481
538
  */
482
539
  hasSession() {
540
+ const isSessionCallBlocked = this._isSessionCallBlocked()
541
+ if (isSessionCallBlocked) {
542
+ return this._session;
543
+ }
544
+
483
545
  if (this._hasSessionInProgress) {
484
546
  return this._hasSessionInProgress;
485
547
  }
548
+
486
549
  const _postProcess = (sessionData) => {
487
550
  if (sessionData.error) {
488
551
  throw new SDKError('HasSession failed', sessionData.error);
@@ -492,6 +555,14 @@ export class Identity extends EventEmitter {
492
555
  this._session = sessionData;
493
556
  return sessionData;
494
557
  };
558
+
559
+ const _checkRedirectionNeed = (sessionData={})=>{
560
+ const sessionDataKeys = Object.keys(sessionData);
561
+
562
+ return sessionDataKeys.length === 1 &&
563
+ sessionDataKeys[0] === 'redirectURL';
564
+ }
565
+
495
566
  const _getSession = async () => {
496
567
  if (this._enableSessionCaching) {
497
568
  // Try to resolve from cache (it has a TTL)
@@ -502,7 +573,7 @@ export class Identity extends EventEmitter {
502
573
  }
503
574
  let sessionData = null;
504
575
  try {
505
- sessionData = await this._sessionService.get('/session');
576
+ sessionData = await this._sessionService.get('/v2/session');
506
577
  } catch (err) {
507
578
  if (err && err.code === 400 && this._enableSessionCaching) {
508
579
  const expiresIn = 1000 * (err.expiresIn || 300);
@@ -511,16 +582,33 @@ export class Identity extends EventEmitter {
511
582
  throw err;
512
583
  }
513
584
 
514
- if (sessionData && this._enableSessionCaching) {
515
- const expiresIn = 1000 * (sessionData.expiresIn || 300);
516
- this.cache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn);
585
+ if (sessionData){
586
+ // for expiring session and safari browser do full page redirect to gain new session
587
+ if(_checkRedirectionNeed(sessionData)){
588
+ this._blockSessionCall();
589
+
590
+ await this.callbackBeforeRedirect();
591
+
592
+ return this._sessionService.makeUrl(sessionData.redirectURL);
593
+ }
594
+
595
+ if (this._enableSessionCaching) {
596
+ const expiresIn = 1000 * (sessionData.expiresIn || 300);
597
+ this.cache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn);
598
+ }
517
599
  }
600
+
518
601
  return _postProcess(sessionData);
519
602
  };
520
603
  this._hasSessionInProgress = _getSession()
521
604
  .then(
522
605
  sessionData => {
523
606
  this._hasSessionInProgress = false;
607
+
608
+ if (isUrl(sessionData)) {
609
+ return this.window.location.href = sessionData;
610
+ }
611
+
524
612
  return sessionData;
525
613
  },
526
614
  err => {
@@ -632,10 +720,10 @@ export class Identity extends EventEmitter {
632
720
  * meaning the same user's ID will differ between merchants.
633
721
  * Additionally, this identifier is bound to the external party provided as argument.
634
722
  *
635
- * @description This function calls {@link Identity#hasSession} internally and thus has the side
636
- * effect that it might perform an auto-login on the user
637
723
  * @param {string} externalParty
638
724
  * @param {string|null} optionalSuffix
725
+ * @description This function calls {@link Identity#hasSession} internally and thus has the side
726
+ * effect that it might perform an auto-login on the user
639
727
  * @throws {SDKError} If the `pairId` is missing in user session.
640
728
  * @throws {SDKError} If the `externalParty` is not defined
641
729
  * @return {Promise<string>} The merchant- and 3rd-party-specific `externalId`
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';
4
+ const version = '5.0.0';
5
5
  export default version;