@nibssplc/cams-sdk-react 0.0.1-beta.75 → 0.0.1-beta.76

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
@@ -236,11 +236,21 @@ function useCAMSAuth(options) {
236
236
  };
237
237
  }
238
238
 
239
+ var CAMSContext$1 = createContext(null);
240
+ function useCAMSContext$1() {
241
+ var context = useContext(CAMSContext$1);
242
+ if (!context) {
243
+ throw new Error("useCAMSContext must be used within a UnifiedCAMSProvider");
244
+ }
245
+ return context;
246
+ }
247
+
239
248
  function useCAMSMSALAuth(options) {
240
249
  var _this = this;
241
250
  var storageKey = options.storageKey || "CAMS-MSAL-AUTH-SDK";
242
251
  var _a = useMsal(), instance = _a.instance, inProgress = _a.inProgress, accounts = _a.accounts;
243
252
  var account = useAccount(accounts[0] || {});
253
+ var setUserProfile = useCAMSContext$1().setUserProfile;
244
254
  var _b = useState(null), error = _b[0], setError = _b[1];
245
255
  var _c = useState(""), idToken = _c[0], setIdToken = _c[1];
246
256
  var _d = useState(""), accessToken = _d[0], setAccessToken = _d[1];
@@ -389,50 +399,41 @@ function useCAMSMSALAuth(options) {
389
399
  }
390
400
  });
391
401
  }); }, [instance, scopes, options]);
392
- var completeMFA = useCallback(function (code, type) { return __awaiter(_this, void 0, void 0, function () {
393
- var mfaResponse, error_1;
402
+ var completeMFA = useCallback(function (data) { return __awaiter(_this, void 0, void 0, function () {
394
403
  return __generator(this, function (_a) {
395
- switch (_a.label) {
396
- case 0:
397
- if (!mfaAuthenticator) {
398
- throw new CAMSError(CAMSErrorType.API_VALIDATION_ERROR, "MFA Authenticator not initialized");
399
- }
400
- _a.label = 1;
401
- case 1:
402
- _a.trys.push([1, 3, , 4]);
403
- return [4 /*yield*/, mfaAuthenticator.verifyOTP(code, type)];
404
- case 2:
405
- mfaResponse = _a.sent();
406
- if (mfaResponse.isAuthenticated) {
407
- // Update storage with complete authentication BEFORE setting state
408
- if (typeof window !== "undefined") {
409
- localStorage.setItem(storageKey, JSON.stringify({
410
- isAuthenticated: true,
411
- requiresMFA: false,
412
- accessToken: accessToken,
413
- idToken: idToken,
414
- userProfile: mfaResponse,
415
- }));
416
- Logger.info("MFA completed successfully, storage updated", {
417
- accessToken: accessToken,
418
- idToken: idToken,
419
- isAuthenticated: isAuthenticated,
420
- requiresMFA: requiresMFA,
421
- });
422
- }
423
- // Set requiresMFA to false after storage update
424
- setRequiresMFA(false);
425
- Logger.debug("MFA completed successfully, requiresMFA set to false");
426
- }
427
- return [2 /*return*/, mfaResponse];
428
- case 3:
429
- error_1 = _a.sent();
430
- setError(error_1);
431
- throw error_1;
432
- case 4: return [2 /*return*/];
404
+ if (!mfaAuthenticator) {
405
+ throw new CAMSError(CAMSErrorType.API_VALIDATION_ERROR, "MFA Authenticator not initialized");
433
406
  }
407
+ Logger.info("Completed MFA.. Setting State");
408
+ try {
409
+ // Update storage with complete authentication BEFORE setting state
410
+ if (typeof window !== "undefined") {
411
+ localStorage.setItem(storageKey, JSON.stringify({
412
+ isAuthenticated: true,
413
+ requiresMFA: false,
414
+ accessToken: accessToken,
415
+ idToken: idToken,
416
+ }));
417
+ setUserProfile({ type: "AUTH_SUCCESS", userProfile: __assign({}, data) });
418
+ setRequiresMFA(false);
419
+ // Set requiresMFA to false after storage update
420
+ Logger.debug("MFA completed successfully, storage updated", {
421
+ accessToken: accessToken,
422
+ idToken: idToken,
423
+ isAuthenticated: true,
424
+ requiresMFA: false,
425
+ });
426
+ }
427
+ Logger.debug("MFA completed successfully, requiresMFA set to false");
428
+ return [2 /*return*/, data];
429
+ }
430
+ catch (error) {
431
+ setError(error);
432
+ throw error;
433
+ }
434
+ return [2 /*return*/];
434
435
  });
435
- }); }, [mfaAuthenticator, accessToken, idToken, storageKey, requiresMFA]);
436
+ }); }, [mfaAuthenticator, accessToken, idToken, storageKey]);
436
437
  var sendEmailOTP = useCallback(function () { return __awaiter(_this, void 0, void 0, function () {
437
438
  return __generator(this, function (_a) {
438
439
  switch (_a.label) {
@@ -487,6 +488,7 @@ function useCAMSMSALAuth(options) {
487
488
  requiresMFA: requiresMFA,
488
489
  completeMFA: completeMFA,
489
490
  sendEmailOTP: sendEmailOTP,
491
+ setRequiresMFA: setRequiresMFA,
490
492
  };
491
493
  }
492
494
 
@@ -959,9 +961,9 @@ if (process.env.NODE_ENV === 'production') {
959
961
 
960
962
  var jsxRuntimeExports = jsxRuntime.exports;
961
963
 
962
- var CAMSContext$1 = createContext(null);
963
- function useCAMSContext$1() {
964
- var context = useContext(CAMSContext$1);
964
+ var CAMSContext = createContext(null);
965
+ function useCAMSContext() {
966
+ var context = useContext(CAMSContext);
965
967
  if (!context) {
966
968
  throw new Error('useCAMSContext must be used within a CAMSProvider');
967
969
  }
@@ -970,7 +972,7 @@ function useCAMSContext$1() {
970
972
 
971
973
  function ProtectedRoute(_a) {
972
974
  var children = _a.children, fallback = _a.fallback, redirectTo = _a.redirectTo;
973
- var _b = useCAMSContext$1(), isAuthenticated = _b.isAuthenticated, isLoading = _b.isLoading;
975
+ var _b = useCAMSContext(), isAuthenticated = _b.isAuthenticated, isLoading = _b.isLoading;
974
976
  if (isLoading) {
975
977
  return fallback || jsxRuntimeExports.jsx("div", { children: "Loading..." });
976
978
  }
@@ -1109,15 +1111,6 @@ var ClientOnly = function (_a) {
1109
1111
  return jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: children });
1110
1112
  };
1111
1113
 
1112
- var CAMSContext = createContext(null);
1113
- function useCAMSContext() {
1114
- var context = useContext(CAMSContext);
1115
- if (!context) {
1116
- throw new Error("useCAMSContext must be used within a UnifiedCAMSProvider");
1117
- }
1118
- return context;
1119
- }
1120
-
1121
1114
  var GuidSchema = z.uuid("appCode must be a valid GUID");
1122
1115
  var setCookie = function (name, value, days) {
1123
1116
  var expires = new Date(Date.now() + days * 24 * 60 * 60 * 1000).toUTCString();
@@ -1229,7 +1222,7 @@ function CAMSProviderCore(props) {
1229
1222
  var value = useMemo(function () {
1230
1223
  return (__assign(__assign({}, auth), { logout: enhancedLogout, userProfile: userProfile, setUserProfile: setUserProfile, authMode: mode }));
1231
1224
  }, [auth, userProfile, mode]);
1232
- return jsxRuntimeExports.jsx(CAMSContext.Provider, { value: value, children: children });
1225
+ return jsxRuntimeExports.jsx(CAMSContext$1.Provider, { value: value, children: children });
1233
1226
  }
1234
1227
  function UnifiedCAMSProvider(props) {
1235
1228
  // Validate appCode is a valid GUID
@@ -1746,8 +1739,8 @@ var MFAOptions = function (_a) {
1746
1739
  var _d = useState(false), otpVisible = _d[0], setOtpVisible = _d[1];
1747
1740
  var _e = useState(false), showSuccessAnimation = _e[0], setShowSuccessAnimation = _e[1];
1748
1741
  var _f = useState(null), authType = _f[0], setAuthType = _f[1];
1749
- var _g = useState(""), lastOTPCode = _g[0], setLastOTPCode = _g[1];
1750
- var context = useCAMSContext();
1742
+ var _g = useState(""); _g[0]; var setLastOTPCode = _g[1];
1743
+ var context = useCAMSContext$1();
1751
1744
  var _h = context.authMode === "MSAL" && "sendEmailOTP" in context
1752
1745
  ? context
1753
1746
  : { sendEmailOTP: null, completeMFA: null, logout: function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
@@ -1763,10 +1756,12 @@ var MFAOptions = function (_a) {
1763
1756
  authenticationType: authType,
1764
1757
  MFAEndpoint: MFAEndpoint,
1765
1758
  onAuthComplete: function (state, data) {
1759
+ console.log("Completed Auth. Handling MFA", state);
1766
1760
  if (state) {
1767
1761
  // Call completeMFA to update context state
1768
- if (completeMFA && authType && lastOTPCode) {
1769
- completeMFA(lastOTPCode, authType).catch(function (error) {
1762
+ if (completeMFA && data) {
1763
+ console.log("IN Complete MFA Block. Handling MFA", data);
1764
+ completeMFA(data).catch(function (error) {
1770
1765
  console.error("Failed to complete MFA:", error);
1771
1766
  });
1772
1767
  }
@@ -1782,7 +1777,7 @@ var MFAOptions = function (_a) {
1782
1777
  onAuthFailed === null || onAuthFailed === void 0 ? void 0 : onAuthFailed();
1783
1778
  onComplete === null || onComplete === void 0 ? void 0 : onComplete(false);
1784
1779
  });
1785
- }, 1000);
1780
+ }, 3000);
1786
1781
  }
1787
1782
  }
1788
1783
  },
@@ -1854,7 +1849,7 @@ var AuthLogo = "data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2
1854
1849
  var MicrosoftLogo = "data:image/svg+xml,%3Csvg%20width%3D%2225%22%20height%3D%2225%22%20viewBox%3D%220%200%2025%2025%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Cg%20id%3D%22microsoft_svgrepo.com%22%3E%3Cpath%20id%3D%22Vector%22%20d%3D%22M20.75%2013.25H13.25V20.75H20.75V13.25Z%22%20fill%3D%22%23FEBA08%22%2F%3E%3Cpath%20id%3D%22Vector_2%22%20d%3D%22M11.75%2013.25H4.25V20.75H11.75V13.25Z%22%20fill%3D%22%2305A6F0%22%2F%3E%3Cpath%20id%3D%22Vector_3%22%20d%3D%22M20.75%204.25H13.25V11.75H20.75V4.25Z%22%20fill%3D%22%2380BC06%22%2F%3E%3Cpath%20id%3D%22Vector_4%22%20d%3D%22M11.75%204.25H4.25V11.75H11.75V4.25Z%22%20fill%3D%22%23F25325%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E";
1855
1850
 
1856
1851
  var DefaultLoginPage = function () {
1857
- var context = useCAMSContext();
1852
+ var context = useCAMSContext$1();
1858
1853
  var login = context.login, isLoading = context.isLoading, authMode = context.authMode;
1859
1854
  var handleLogin = function () {
1860
1855
  if (authMode === "MSAL") {
@@ -1882,44 +1877,52 @@ var DefaultLoginPage = function () {
1882
1877
  className: "w-full flex items-center justify-center cursor-pointer bg-[#506f4a] hover:bg-[#506f4a] rounded-lg border border-transparent px-5 py-8 text-base font-medium transition-colors duration-250", onClick: handleLogin, disabled: isLoading, children: [jsxRuntimeExports.jsx("img", { src: MicrosoftLogo, alt: "Microsoft Logo", width: 35, height: 35 }), jsxRuntimeExports.jsx("span", { children: isLoading ? 'Logging in...' : "Sign in with Microsoft" })] }) }), jsxRuntimeExports.jsxs(CardFooter, { className: "flex items-center justify-center mt-6 space-x-2 text-gray-400 text-sm", children: [jsxRuntimeExports.jsx(ShieldCheck, { className: "w-4 h-4 text-[#506f4a] pulse-glow" }), jsxRuntimeExports.jsx("span", { children: "Powered By NIBSS" })] })] }) }) }, "landing") }));
1883
1878
  };
1884
1879
 
1880
+ var ErrorFallback = function (_a) {
1881
+ var message = _a.message;
1882
+ return (jsxRuntimeExports.jsx("div", { className: "flex items-center justify-center min-h-screen bg-gray-50", children: jsxRuntimeExports.jsxs("div", { className: "bg-white p-6 rounded-lg shadow-lg border border-red-200 max-w-md", children: [jsxRuntimeExports.jsxs("div", { className: "flex items-center gap-3 mb-2", children: [jsxRuntimeExports.jsx("svg", { className: "w-6 h-6 text-red-500", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24", children: jsxRuntimeExports.jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: 2, d: "M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" }) }), jsxRuntimeExports.jsx("h2", { className: "text-lg font-semibold text-gray-900", children: "Configuration Error" })] }), jsxRuntimeExports.jsx("p", { className: "text-red-600", children: message })] }) }));
1883
+ };
1884
+
1885
1885
  var MFAEndpointUrlSchema = z$1.url("MFAEndpoint must be a valid URL");
1886
1886
  var MFAGate = function (_a) {
1887
- var children = _a.children, fallback = _a.fallback, LoginComponent = _a.loginComponent, MFAEndpoint = _a.MFAEndpoint;
1888
- var context = useCAMSContext();
1889
- var urlValidation = MFAEndpointUrlSchema.safeParse(MFAEndpoint);
1890
- if (!urlValidation.success) {
1891
- throw new Error("Invalid MFAEndpoint: ".concat(urlValidation.error.issues[0].message));
1892
- }
1887
+ var children = _a.children, _b = _a.fallback, fallback = _b === void 0 ? jsxRuntimeExports.jsx(LoadingSpinner, {}) : _b, _c = _a.loginComponent, LoginComponent = _c === void 0 ? DefaultLoginPage : _c, MFAEndpoint = _a.MFAEndpoint;
1888
+ var context = useCAMSContext$1();
1889
+ var validatedMFAEndpoint = useMemo(function () {
1890
+ var parsed = MFAEndpointUrlSchema.safeParse(MFAEndpoint);
1891
+ if (!parsed.success) {
1892
+ Logger.error("Invalid MFAEndpoint", parsed.error);
1893
+ return null;
1894
+ }
1895
+ return parsed.data;
1896
+ }, [MFAEndpoint]);
1897
+ var handleComplete = useCallback(function (success) {
1898
+ if (!success)
1899
+ Logger.error("MFA authentication failed");
1900
+ }, []);
1901
+ var handleAuthFailed = useCallback(function () { return __awaiter(void 0, void 0, void 0, function () {
1902
+ return __generator(this, function (_a) {
1903
+ switch (_a.label) {
1904
+ case 0: return [4 /*yield*/, context.logout()];
1905
+ case 1:
1906
+ _a.sent();
1907
+ return [2 /*return*/];
1908
+ }
1909
+ });
1910
+ }); }, [context.logout]);
1893
1911
  Logger.debug("MFA Endpoint >>>", { MFAEndpoint: MFAEndpoint });
1894
- // Only apply MFA gate for MSAL mode
1895
- if (context.authMode !== "MSAL") {
1912
+ if (!validatedMFAEndpoint)
1913
+ return jsxRuntimeExports.jsx(ErrorFallback, { message: "Invalid MFA configuration." });
1914
+ if (context.authMode !== "MSAL")
1896
1915
  return jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: children });
1916
+ if (context.isLoading)
1917
+ return fallback;
1918
+ if (context.requiresMFA) {
1919
+ return (jsxRuntimeExports.jsx(MFAOptions, { MFAEndpoint: validatedMFAEndpoint, onComplete: handleComplete, onAuthFailed: handleAuthFailed }));
1897
1920
  }
1898
- var requiresMFA = context.requiresMFA, isAuthenticated = context.isAuthenticated, isLoading = context.isLoading;
1899
- if (isLoading) {
1900
- return fallback || jsxRuntimeExports.jsx("div", { className: "flex h-dvh items-center justify-center", children: "Loading..." });
1901
- }
1902
- if (requiresMFA) {
1903
- return (jsxRuntimeExports.jsx(MFAOptions, { MFAEndpoint: MFAEndpoint, onComplete: function (success) {
1904
- if (!success) {
1905
- console.error("MFA authentication failed");
1906
- }
1907
- }, onAuthFailed: function () { return __awaiter(void 0, void 0, void 0, function () {
1908
- return __generator(this, function (_a) {
1909
- switch (_a.label) {
1910
- case 0: return [4 /*yield*/, context.logout()];
1911
- case 1:
1912
- _a.sent();
1913
- return [2 /*return*/];
1914
- }
1915
- });
1916
- }); } }));
1917
- }
1918
- if (!isAuthenticated) {
1919
- return LoginComponent ? jsxRuntimeExports.jsx(LoginComponent, {}) : jsxRuntimeExports.jsx(DefaultLoginPage, {});
1921
+ if (!context.isAuthenticated) {
1922
+ return jsxRuntimeExports.jsx(LoginComponent, {});
1920
1923
  }
1921
1924
  return jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: children });
1922
1925
  };
1923
1926
 
1924
- export { CAMSMSALProvider, CAMSProvider, ClientOnly, DefaultLoginPage as LoginButton, MFAGate, MFAOptions, ProtectedRoute, UnifiedCAMSProvider, useCAMSAuth, useCAMSContext, useCAMSMSALAuth, useCAMSMSALContext, useCAMSPopupAuth };
1927
+ export { CAMSMSALProvider, CAMSProvider, ClientOnly, DefaultLoginPage as LoginButton, MFAGate, MFAOptions, ProtectedRoute, UnifiedCAMSProvider, useCAMSAuth, useCAMSContext$1 as useCAMSContext, useCAMSMSALAuth, useCAMSMSALContext, useCAMSPopupAuth };
1925
1928
  //# sourceMappingURL=index.esm.js.map