@nibssplc/cams-sdk-react 1.0.0-rc.17 → 1.0.0-rc.19

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.
@@ -11,7 +11,6 @@ export interface CAMSMSALProviderProps extends UseCAMSMSALAuthOptions {
11
11
  children: React.ReactNode;
12
12
  msalConfig: Configuration;
13
13
  msalInstance?: PublicClientApplication;
14
- activeCookiePeriod?: number;
15
14
  }
16
15
  export declare function CAMSMSALProvider(props: Readonly<CAMSMSALProviderProps>): import("react/jsx-runtime").JSX.Element;
17
16
  export declare function useCAMSMSALContext(): CAMSMSALContextValue;
@@ -28,5 +28,6 @@ export interface UseCAMSMSALAuthReturn {
28
28
  completeMFA: (data: UserValidatedResponse) => Promise<MFAResponse>;
29
29
  setRequiresMFA: React.Dispatch<React.SetStateAction<boolean>>;
30
30
  sendEmailOTP: () => Promise<boolean>;
31
+ activeCookiePeriod: number;
31
32
  }
32
33
  export declare function useCAMSMSALAuth(options: UseCAMSMSALAuthOptions): UseCAMSMSALAuthReturn;
package/dist/index.cjs.js CHANGED
@@ -404,17 +404,18 @@ var deleteCookie$1 = function (name) {
404
404
 
405
405
  function useCAMSMSALAuth(options) {
406
406
  var _this = this;
407
- var storageKey = options.storageKey || "CAMS-MSAL-AUTH-SDK";
408
- var _a = msalReact.useMsal(), instance = _a.instance, inProgress = _a.inProgress, accounts = _a.accounts;
407
+ var optStorageKey = options.storageKey, optScopes = options.scopes, prompt = options.prompt, appCode = options.appCode, MFAEndpoint = options.MFAEndpoint, onAuthSuccess = options.onAuthSuccess, onAuthError = options.onAuthError, _a = options.activeCookiePeriod, activeCookiePeriod = _a === void 0 ? 1 : _a;
408
+ var storageKey = optStorageKey || "CAMS-MSAL-AUTH-SDK";
409
+ var _b = msalReact.useMsal(), instance = _b.instance, inProgress = _b.inProgress, accounts = _b.accounts;
409
410
  var account = msalReact.useAccount(accounts[0] || {});
410
- var _b = React.useState(null), error = _b[0], setError = _b[1];
411
- var _c = React.useState(""), idToken = _c[0], setIdToken = _c[1];
412
- var _d = React.useState(""), accessToken = _d[0], setAccessToken = _d[1];
413
- var _e = React.useState(null), mfaAuthenticator = _e[0], setMfaAuthenticator = _e[1];
414
- var _f = React.useState(false), requiresMFA = _f[0], setRequiresMFA = _f[1];
411
+ var _c = React.useState(null), error = _c[0], setError = _c[1];
412
+ var _d = React.useState(""), idToken = _d[0], setIdToken = _d[1];
413
+ var _e = React.useState(""), accessToken = _e[0], setAccessToken = _e[1];
414
+ var _f = React.useState(null), mfaAuthenticator = _f[0], setMfaAuthenticator = _f[1];
415
+ var _g = React.useState(false), requiresMFA = _g[0], setRequiresMFA = _g[1];
415
416
  var isLoading = inProgress !== msalBrowser.InteractionStatus.None;
416
417
  var isAuthenticated = !!account && !!accessToken && !requiresMFA;
417
- var scopes = options.scopes || ["openid", "profile", "email"];
418
+ var scopes = optScopes || ["openid", "profile", "email"];
418
419
  var isTokenValid = function (token) {
419
420
  try {
420
421
  var payload = JSON.parse(atob(token.split(".")[1]));
@@ -438,9 +439,9 @@ function useCAMSMSALAuth(options) {
438
439
  var mfaConfig = {
439
440
  accessToken: accessToken_1,
440
441
  idToken: idToken_1,
441
- appCode: options.appCode,
442
+ appCode: appCode,
442
443
  provider: "MSAL",
443
- apiEndpoint: options.MFAEndpoint,
444
+ apiEndpoint: MFAEndpoint,
444
445
  };
445
446
  var authenticator = new camsSdk.CAMSMFAAuthenticator(mfaConfig);
446
447
  setMfaAuthenticator(authenticator);
@@ -489,24 +490,24 @@ function useCAMSMSALAuth(options) {
489
490
  // }, []);
490
491
  var login = React.useCallback(function () { return __awaiter$1(_this, void 0, void 0, function () {
491
492
  var response, mfaConfig, authenticator, err_1, camsError_1, camsError;
492
- var _a, _b, _c, _d;
493
- return __generator$1(this, function (_e) {
494
- switch (_e.label) {
493
+ var _a;
494
+ return __generator$1(this, function (_b) {
495
+ switch (_b.label) {
495
496
  case 0:
496
497
  if (inProgress !== msalBrowser.InteractionStatus.None) {
497
498
  camsSdk.Logger.warn("Authentication already in progress, ignoring duplicate call");
498
499
  return [2 /*return*/];
499
500
  }
500
501
  setError(null);
501
- _e.label = 1;
502
+ _b.label = 1;
502
503
  case 1:
503
- _e.trys.push([1, 3, , 4]);
504
+ _b.trys.push([1, 3, , 4]);
504
505
  return [4 /*yield*/, instance.loginPopup({
505
506
  scopes: scopes,
506
- prompt: options.prompt || "login",
507
+ prompt: prompt || "login",
507
508
  })];
508
509
  case 2:
509
- response = _e.sent();
510
+ response = _b.sent();
510
511
  console.log("Login Token response:", {
511
512
  accessToken: response.accessToken,
512
513
  idToken: response.idToken,
@@ -516,9 +517,9 @@ function useCAMSMSALAuth(options) {
516
517
  mfaConfig = {
517
518
  accessToken: response.accessToken,
518
519
  idToken: response.idToken,
519
- appCode: options.appCode,
520
+ appCode: appCode,
520
521
  provider: "MSAL",
521
- apiEndpoint: options.MFAEndpoint,
522
+ apiEndpoint: MFAEndpoint,
522
523
  };
523
524
  authenticator = new camsSdk.CAMSMFAAuthenticator(mfaConfig);
524
525
  setMfaAuthenticator(authenticator);
@@ -532,10 +533,10 @@ function useCAMSMSALAuth(options) {
532
533
  idToken: response.idToken,
533
534
  }));
534
535
  }
535
- (_a = options.onAuthSuccess) === null || _a === void 0 ? void 0 : _a.call(options, response.accessToken);
536
+ onAuthSuccess === null || onAuthSuccess === void 0 ? void 0 : onAuthSuccess(response.accessToken);
536
537
  return [3 /*break*/, 4];
537
538
  case 3:
538
- err_1 = _e.sent();
539
+ err_1 = _b.sent();
539
540
  // Handle interaction_in_progress error
540
541
  if (err_1.errorCode === "interaction_in_progress") {
541
542
  camsSdk.Logger.warn("Interaction already in progress, please wait");
@@ -549,20 +550,20 @@ function useCAMSMSALAuth(options) {
549
550
  }
550
551
  // If popup is blocked
551
552
  if (err_1.errorCode === "popup_window_error" ||
552
- ((_b = err_1.message) === null || _b === void 0 ? void 0 : _b.includes("popup"))) {
553
+ ((_a = err_1.message) === null || _a === void 0 ? void 0 : _a.includes("popup"))) {
553
554
  camsError_1 = new camsSdk.CAMSError(camsSdk.CAMSErrorType.POPUP_BLOCKED, "Popup blocked by browser. Please allow popups and try again.");
554
555
  setError(camsError_1);
555
- (_c = options.onAuthError) === null || _c === void 0 ? void 0 : _c.call(options, camsError_1);
556
+ onAuthError === null || onAuthError === void 0 ? void 0 : onAuthError(camsError_1);
556
557
  return [2 /*return*/];
557
558
  }
558
559
  camsError = new camsSdk.CAMSError(camsSdk.CAMSErrorType.API_VALIDATION_ERROR, "Login failed: " + err_1.message || err_1);
559
560
  setError(camsError);
560
- (_d = options.onAuthError) === null || _d === void 0 ? void 0 : _d.call(options, camsError);
561
+ onAuthError === null || onAuthError === void 0 ? void 0 : onAuthError(camsError);
561
562
  return [3 /*break*/, 4];
562
563
  case 4: return [2 /*return*/];
563
564
  }
564
565
  });
565
- }); }, [instance, scopes, options, inProgress]);
566
+ }); }, [instance, scopes, prompt, appCode, MFAEndpoint, onAuthSuccess, onAuthError, storageKey, inProgress]);
566
567
  var completeMFA = React.useCallback(function (data) { return __awaiter$1(_this, void 0, void 0, function () {
567
568
  return __generator$1(this, function (_a) {
568
569
  if (!mfaAuthenticator) {
@@ -578,7 +579,7 @@ function useCAMSMSALAuth(options) {
578
579
  accessToken: accessToken,
579
580
  idToken: idToken,
580
581
  }));
581
- setCookie$1("CAMS-MSAL-AUTH-SDK-PROFILE", JSON.stringify({ type: "AUTH_SUCCESS", userProfile: __assign({}, data) }), options.activeCookiePeriod);
582
+ setCookie$1("CAMS-MSAL-AUTH-SDK-PROFILE", JSON.stringify({ type: "AUTH_SUCCESS", userProfile: __assign({}, data) }), activeCookiePeriod);
582
583
  setRequiresMFA(false);
583
584
  // Set requiresMFA to false after storage update
584
585
  camsSdk.Logger.debug("MFA completed successfully, storage updated", {
@@ -597,7 +598,7 @@ function useCAMSMSALAuth(options) {
597
598
  }
598
599
  return [2 /*return*/];
599
600
  });
600
- }); }, [mfaAuthenticator, accessToken, idToken, storageKey]);
601
+ }); }, [mfaAuthenticator, accessToken, idToken, storageKey, activeCookiePeriod]);
601
602
  var sendEmailOTP = React.useCallback(function () { return __awaiter$1(_this, void 0, void 0, function () {
602
603
  return __generator$1(this, function (_a) {
603
604
  switch (_a.label) {
@@ -648,12 +649,13 @@ function useCAMSMSALAuth(options) {
648
649
  error: error,
649
650
  idToken: idToken,
650
651
  accessToken: accessToken,
651
- appCode: options.appCode,
652
+ appCode: appCode,
652
653
  mfaAuthenticator: mfaAuthenticator,
653
654
  requiresMFA: requiresMFA,
654
655
  completeMFA: completeMFA,
655
656
  sendEmailOTP: sendEmailOTP,
656
657
  setRequiresMFA: setRequiresMFA,
658
+ activeCookiePeriod: activeCookiePeriod,
657
659
  };
658
660
  }
659
661
 
@@ -1322,7 +1324,7 @@ function CAMSMSALProviderInner(_a) {
1322
1324
  React.useEffect(function () {
1323
1325
  if (typeof window !== "undefined") {
1324
1326
  if (userProfile) {
1325
- setCookie(profileStorageKey, JSON.stringify(userProfile), authOptions.activeCookiePeriod); // Store for 1 day
1327
+ setCookie(profileStorageKey, JSON.stringify(userProfile), auth.activeCookiePeriod); // Store for 1 day
1326
1328
  }
1327
1329
  else {
1328
1330
  deleteCookie(profileStorageKey);