@nibssplc/cams-sdk-react 1.0.0-rc.133 → 1.0.0-rc.134

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/dist/index.cjs.js CHANGED
@@ -415,15 +415,42 @@ function useCAMSMSALAuth(options) {
415
415
  var optStorageKey = options.storageKey, optScopes = options.scopes, prompt = options.prompt, appCode = options.appCode, ValidateUserEndpoint = options.ValidateUserEndpoint, _a = options.activeCookiePeriod, activeCookiePeriod = _a === void 0 ? 1 : _a, _b = options.DisableCAMSUserProfileValidation, DisableCAMSUserProfileValidation = _b === void 0 ? false : _b;
416
416
  var storageKey = optStorageKey || "CAMS-MSAL-AUTH-SDK";
417
417
  var _c = msalReact.useMsal(), instance = _c.instance, inProgress = _c.inProgress, accounts = _c.accounts;
418
- var account = msalReact.useAccount(accounts[0] || {});
418
+ msalReact.useAccount(accounts[0] || {});
419
+ var isTokenValid = function (token) {
420
+ try {
421
+ var payload = JSON.parse(atob(token.split(".")[1]));
422
+ return payload.exp * 1000 > Date.now();
423
+ }
424
+ catch (_a) {
425
+ return false;
426
+ }
427
+ };
428
+ var readFromStorage = function () {
429
+ var _a, _b, _c;
430
+ if (typeof window === "undefined")
431
+ return { accessToken: "", idToken: "", email: "" };
432
+ try {
433
+ var raw = localStorage.getItem(storageKey);
434
+ if (!raw)
435
+ return { accessToken: "", idToken: "", email: "" };
436
+ var parsed = JSON.parse(raw);
437
+ var token = (_a = parsed.accessToken) !== null && _a !== void 0 ? _a : "";
438
+ return isTokenValid(token)
439
+ ? { accessToken: token, idToken: (_b = parsed.idToken) !== null && _b !== void 0 ? _b : "", email: (_c = parsed.email) !== null && _c !== void 0 ? _c : "" }
440
+ : { accessToken: "", idToken: "", email: "" };
441
+ }
442
+ catch (_d) {
443
+ return { accessToken: "", idToken: "", email: "" };
444
+ }
445
+ };
419
446
  var _d = React.useState(null), error = _d[0], setError = _d[1];
420
- var _e = React.useState(""), idToken = _e[0], setIdToken = _e[1];
421
- var _f = React.useState(""), accessToken = _f[0], setAccessToken = _f[1];
422
- var _g = React.useState(""), email = _g[0], setEmail = _g[1];
447
+ var _e = React.useState(function () { return readFromStorage().idToken; }), idToken = _e[0], setIdToken = _e[1];
448
+ var _f = React.useState(function () { return readFromStorage().accessToken; }), accessToken = _f[0], setAccessToken = _f[1];
449
+ var _g = React.useState(function () { return readFromStorage().email; }), email = _g[0], setEmail = _g[1];
423
450
  var _h = React.useState(false), requiresMFA = _h[0], setRequiresMFA = _h[1];
424
451
  var _j = React.useState(false), isFetchingMFAConfig = _j[0], setIsFetchingMFAConfig = _j[1];
425
452
  var isLoading = inProgress !== msalBrowser.InteractionStatus.None || isFetchingMFAConfig;
426
- var isAuthenticated = !!account && !!accessToken && !requiresMFA;
453
+ var isAuthenticated = !!accessToken && !requiresMFA;
427
454
  var scopes = optScopes || ["openid", "profile", "email"];
428
455
  var login = React.useCallback(function () { return __awaiter$1(_this, void 0, void 0, function () {
429
456
  var response, authenticator, email_1, userConfig, camsError, error_1, err_1, camsError_1, camsError;
@@ -452,6 +479,7 @@ function useCAMSMSALAuth(options) {
452
479
  setIdToken(response.idToken);
453
480
  setEmail(email_1);
454
481
  setRequiresMFA(false);
482
+ localStorage.setItem(storageKey, JSON.stringify({ accessToken: response.accessToken, idToken: response.idToken, email: email_1, appCode: appCode }));
455
483
  camsSdk.Logger.info("CAMS User Profile Validation Disabled. Skipping MFA config fetch.");
456
484
  return [3 /*break*/, 7];
457
485
  case 3:
@@ -478,6 +506,7 @@ function useCAMSMSALAuth(options) {
478
506
  setIdToken(response.idToken);
479
507
  setEmail(userConfig.userInfo.email);
480
508
  setRequiresMFA(userConfig.userInfo.isMFAEnabled);
509
+ localStorage.setItem(storageKey, JSON.stringify({ accessToken: response.accessToken, idToken: response.idToken, email: userConfig.userInfo.email, appCode: appCode }));
481
510
  }
482
511
  else {
483
512
  camsError = new camsSdk.CAMSError(camsSdk.CAMSErrorType.API_VALIDATION_ERROR, "Login failed: " + userConfig.message);
@@ -575,6 +604,8 @@ function useCAMSMSALAuth(options) {
575
604
  setEmail("");
576
605
  setError(null);
577
606
  setRequiresMFA(false);
607
+ if (typeof window !== "undefined")
608
+ localStorage.removeItem(storageKey);
578
609
  }
579
610
  catch (err) {
580
611
  camsError = new camsSdk.CAMSError(camsSdk.CAMSErrorType.API_VALIDATION_ERROR, "Logout failed: " + err);