@nibssplc/cams-sdk-react 1.0.0-rc.70 → 1.0.0-rc.72

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.esm.js CHANGED
@@ -419,9 +419,10 @@ function useCAMSMSALAuth(options) {
419
419
  var _c = useState(null), error = _c[0], setError = _c[1];
420
420
  var _d = useState(""), idToken = _d[0], setIdToken = _d[1];
421
421
  var _e = useState(""), accessToken = _e[0], setAccessToken = _e[1];
422
+ var _f = useState(""), email = _f[0], setEmail = _f[1];
422
423
  // const [mfaAuthenticator, setMfaAuthenticator] =
423
424
  // useState<CAMSMFAAuthenticator | null>(null);
424
- var _f = useState(false), requiresMFA = _f[0], setRequiresMFA = _f[1];
425
+ var _g = useState(false), requiresMFA = _g[0], setRequiresMFA = _g[1];
425
426
  var isLoading = inProgress !== InteractionStatus.None;
426
427
  var isAuthenticated = !!account && !!accessToken && !requiresMFA;
427
428
  var scopes = optScopes || ["openid", "profile", "email"];
@@ -500,17 +501,17 @@ function useCAMSMSALAuth(options) {
500
501
  return [4 /*yield*/, authenticator.GetUserMFAConfig(mfaConfig)];
501
502
  case 3:
502
503
  userConfig = _b.sent();
503
- Logger.debug("MFA Authenticator initialized:", userConfig);
504
- console.log("MFA Authenticator initialized:", userConfig);
504
+ Logger.info("MFA Authenticator initialized:", userConfig);
505
505
  // Don't persist as authenticated until MFA is complete
506
506
  if (typeof window !== "undefined" && userConfig.isValid) {
507
- setRequiresMFA(userConfig.userInfo.isMFAEnabled);
508
507
  localStorage.setItem(storageKey, JSON.stringify({
509
508
  isAuthenticated: false,
510
509
  requiresMFA: userConfig.userInfo.isMFAEnabled,
511
510
  accessToken: response.accessToken,
512
511
  idToken: response.idToken,
513
512
  }));
513
+ setEmail(userConfig.userInfo.email);
514
+ setRequiresMFA(userConfig.userInfo.isMFAEnabled);
514
515
  }
515
516
  return [3 /*break*/, 5];
516
517
  case 4:
@@ -630,6 +631,7 @@ function useCAMSMSALAuth(options) {
630
631
  isAuthenticated: isAuthenticated,
631
632
  isLoading: isLoading,
632
633
  error: error,
634
+ email: email,
633
635
  idToken: idToken,
634
636
  accessToken: accessToken,
635
637
  appCode: appCode,
@@ -1521,59 +1523,33 @@ var useCredentialsHandler = function (onAuthComplete) {
1521
1523
  };
1522
1524
 
1523
1525
  var MFAOptions = function (_a) {
1524
- var _b, _c;
1526
+ var _b, _c, _d, _e, _f;
1525
1527
  var onComplete = _a.onComplete, onAuthFailed = _a.onAuthFailed, MFAEndpoints = _a.MFAEndpoints, usePassKey = _a.usePassKey;
1526
- var _d = useState(""), value = _d[0], setValue = _d[1];
1527
- var _e = useState(false), otpVisible = _e[0], setOtpVisible = _e[1];
1528
- var _f = useState(false), showSuccessAnimation = _f[0], setShowSuccessAnimation = _f[1];
1529
- var _g = useState(null), authType = _g[0], setAuthType = _g[1];
1528
+ var _g = useState(""), value = _g[0], setValue = _g[1];
1529
+ var _h = useState(false), otpVisible = _h[0], setOtpVisible = _h[1];
1530
+ var _j = useState(false), showSuccessAnimation = _j[0], setShowSuccessAnimation = _j[1];
1531
+ var _k = useState(null), authType = _k[0], setAuthType = _k[1];
1530
1532
  var context = useCAMSContext();
1531
- var _h = context.authMode === "MSAL" && "sendEmailOTP" in context
1533
+ var _l = context.authMode === "MSAL" && "sendEmailOTP" in context
1532
1534
  ? context
1533
- : { sendEmailOTP: null, completeMFA: null, logout: function () { return __awaiter$1(void 0, void 0, void 0, function () { return __generator$1(this, function (_a) {
1535
+ : { completeMFA: null, logout: function () { return __awaiter$1(void 0, void 0, void 0, function () { return __generator$1(this, function (_a) {
1534
1536
  return [2 /*return*/];
1535
- }); }); } }; _h.sendEmailOTP; var completeMFA = _h.completeMFA, logout = _h.logout;
1537
+ }); }); } }, completeMFA = _l.completeMFA, logout = _l.logout;
1536
1538
  context.authMode === "MSAL" ? context.accessToken : "";
1537
1539
  context.authMode === "MSAL" ? context.idToken : "";
1538
1540
  var authenticate = useWebAuthn().authenticate;
1539
1541
  var authenticator = new CAMSMFAAuthenticator();
1540
- // const handleFIDOLogin = async () => {
1541
- // try {
1542
- // // 1. Fetch authentication challenge from your server
1543
- // console.log("Requesting authentication challenge from server...");
1544
- // const { data: options } = await axiosInstance.post(
1545
- // MFAEndpoints.RetrieveAuthChallenge,
1546
- // {}
1547
- // );
1548
- // console.log("Received challenge:", options);
1549
- // // 2. Call the SDK to trigger the browser's passkey authentication UI
1550
- // console.log("Calling SDK authenticate function...");
1551
- // const assertionResponse = await authenticate({
1552
- // ...options,
1553
- // userVerification: "discouraged",
1554
- // });
1555
- // console.log(
1556
- // "Authentication assertion received from client:",
1557
- // assertionResponse
1558
- // );
1559
- // // 3. Send the assertion back to the server for verification
1560
- // console.log("Sending assertion to server for verification...");
1561
- // await axiosInstance.post(
1562
- // MFAEndpoints.AuthChallengeVerify,
1563
- // assertionResponse
1564
- // );
1565
- // toast.success("🔑 Sign-in successful!");
1566
- // } catch (error) {
1567
- // console.error("Authentication failed:", error);
1568
- // toast.error("❌ Could not sign in.");
1569
- // }
1570
- // };
1571
- var _j = useOTPHandler({
1572
- email: ((_c = (_b = context.user) === null || _b === void 0 ? void 0 : _b.profile) === null || _c === void 0 ? void 0 : _c.email) || "",
1542
+ Logger.debug("User context:", {
1543
+ user: context.user,
1544
+ profile: (_b = context.user) === null || _b === void 0 ? void 0 : _b.profile,
1545
+ email: (_d = (_c = context.user) === null || _c === void 0 ? void 0 : _c.profile) === null || _d === void 0 ? void 0 : _d.email,
1546
+ });
1547
+ var _m = useOTPHandler({
1548
+ email: ((_f = (_e = context.user) === null || _e === void 0 ? void 0 : _e.profile) === null || _f === void 0 ? void 0 : _f.email) || context.email,
1573
1549
  appCode: context.appCode || "",
1574
1550
  MFAEndpoint: MFAEndpoints.ValidateUserMFA,
1575
1551
  onAuthComplete: function (state, data) {
1576
- console.log("Completed Auth. Handling MFA", state);
1552
+ console.log("Completed Auth. Handling MFA", state, data);
1577
1553
  if (state) {
1578
1554
  // Call completeMFA to update context state
1579
1555
  if (completeMFA && data) {
@@ -1598,7 +1574,7 @@ var MFAOptions = function (_a) {
1598
1574
  }
1599
1575
  }
1600
1576
  },
1601
- }), handleSubmitOTP = _j.handleSubmitOTP, loading = _j.loading, attemptCount = _j.attemptCount, remainingAttempts = _j.remainingAttempts, isMaxAttemptsReached = _j.isMaxAttemptsReached, resetAttempts = _j.resetAttempts;
1577
+ }), handleSubmitOTP = _m.handleSubmitOTP, loading = _m.loading, attemptCount = _m.attemptCount, remainingAttempts = _m.remainingAttempts, isMaxAttemptsReached = _m.isMaxAttemptsReached, resetAttempts = _m.resetAttempts;
1602
1578
  var handleGoBack = function () {
1603
1579
  setAuthType(null);
1604
1580
  setOtpVisible(false);
@@ -1803,7 +1779,6 @@ var ErrorFallback = function (_a) {
1803
1779
  };
1804
1780
 
1805
1781
  var MFAEndpointsSchema = z$1.object({
1806
- ValidateUser: z$1.url("ValidateUser Endpoint must be a valid URL"),
1807
1782
  ValidateUserMFA: z$1.url("ValidateUser MFA Endpoint must be a valid URL"),
1808
1783
  RegisterNewChallenge: z$1.url("MFA RegisterNewChallenge must be a valid URL"),
1809
1784
  RegisterVerify: z$1.url("MFA RegisterVerify must be a valid URL"),
@@ -1817,6 +1792,7 @@ var MFAGate = function (_a) {
1817
1792
  // loginComponent: LoginComponent = DefaultLoginPage,
1818
1793
  usePassKey = _c === void 0 ? false : _c, _d = _a.useADLogin, useADLogin = _d === void 0 ? false : _d, CredentialsAuthEndpoint = _a.CredentialsAuthEndpoint, PassKeysRegisterProps = _a.PassKeysRegisterProps, MFAEndpoints = _a.MFAEndpoints, requiresMFA = _a.requiresMFA, onAuthSuccess = _a.onAuthSuccess, onAuthError = _a.onAuthError;
1819
1794
  var context = useCAMSContext();
1795
+ var _e = useState("Loading"), authState = _e[0], setAuthState = _e[1];
1820
1796
  var validatedMFAEndpoints = useMemo(function () {
1821
1797
  var parsed = MFAEndpointsSchema.safeParse(MFAEndpoints);
1822
1798
  if (!parsed.success) {
@@ -1861,30 +1837,50 @@ var MFAGate = function (_a) {
1861
1837
  });
1862
1838
  }); }, [context, onAuthError]);
1863
1839
  useEffect(function () {
1864
- if (requiresMFA === false &&
1865
- "accessToken" in context &&
1866
- context.accessToken &&
1867
- "setRequiresMFA" in context) {
1868
- context.setRequiresMFA(false);
1840
+ if (context.isLoading) {
1841
+ setAuthState("Loading");
1842
+ return;
1843
+ }
1844
+ if (!context.isAuthenticated) {
1845
+ setAuthState("Unauthenticated");
1846
+ return;
1869
1847
  }
1870
- }, [requiresMFA, context]);
1848
+ // Small delay to ensure all context updates are processed
1849
+ var timer = setTimeout(function () {
1850
+ var shouldRequireMFA = requiresMFA !== null && requiresMFA !== void 0 ? requiresMFA : ("requiresMFA" in context ? context.requiresMFA : false);
1851
+ var accessToken = "accessToken" in context ? context.accessToken : "";
1852
+ if (shouldRequireMFA && accessToken) {
1853
+ setAuthState("MFA_Required");
1854
+ }
1855
+ else {
1856
+ setAuthState("Authenticated");
1857
+ }
1858
+ }, 50);
1859
+ return function () { return clearTimeout(timer); };
1860
+ }, [
1861
+ context.isLoading,
1862
+ context.isAuthenticated,
1863
+ // context["requiresMFA"],
1864
+ requiresMFA,
1865
+ context,
1866
+ ]);
1871
1867
  if (useADLogin && !CredentialsAuthEndpoint)
1872
1868
  return jsx(ErrorFallback, { message: "Invalid AD Login Configuration." });
1873
1869
  if (!validatedMFAEndpoints)
1874
1870
  return jsx(ErrorFallback, { message: "Invalid MFA Configuration." });
1875
1871
  if (context.authMode !== "MSAL")
1876
1872
  return jsx(Fragment, { children: children });
1877
- if (context.isLoading)
1878
- return fallback;
1879
- if (!context.isAuthenticated) {
1880
- var shouldRequireMFA = requiresMFA !== null && requiresMFA !== void 0 ? requiresMFA : ("requiresMFA" in context ? context.requiresMFA : false);
1881
- var accessToken = "accessToken" in context ? context.accessToken : "";
1882
- if (shouldRequireMFA && accessToken) {
1873
+ switch (authState) {
1874
+ case "Loading":
1875
+ return fallback;
1876
+ case "MFA_Required":
1883
1877
  return (jsx(MFAOptions, { MFAEndpoints: validatedMFAEndpoints, usePassKey: usePassKey, onComplete: handleComplete, onAuthFailed: handleAuthFailed }));
1884
- }
1885
- return (jsx(DefaultLoginPage, { usePassKey: usePassKey, useADLogin: useADLogin, MFAEndpoints: MFAEndpoints, PassKeysRegisterProps: PassKeysRegisterProps, CredentialsAuthEndpoint: CredentialsAuthEndpoint }));
1878
+ case "Authenticated":
1879
+ return jsx(Fragment, { children: children });
1880
+ case "Unauthenticated":
1881
+ default:
1882
+ return (jsx(DefaultLoginPage, { usePassKey: usePassKey, useADLogin: useADLogin, MFAEndpoints: MFAEndpoints, PassKeysRegisterProps: PassKeysRegisterProps, CredentialsAuthEndpoint: CredentialsAuthEndpoint }));
1886
1883
  }
1887
- return jsx(Fragment, { children: children });
1888
1884
  };
1889
1885
 
1890
1886
  export { CAMSMSALProvider, CAMSProvider, ClientOnly, DefaultLoginPage, MFAGate, MFAOptions, ProtectedRoute, UnifiedCAMSProvider, authenticate, register, useCAMSAuth, useCAMSContext, useCAMSMSALAuth, useCAMSMSALContext, useWebAuthn };