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

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;
@@ -446,8 +473,9 @@ function useCAMSMSALAuth(options) {
446
473
  case 2:
447
474
  response = _e.sent();
448
475
  authenticator = new camsSdk.CAMSMFAAuthenticator();
449
- if (!DisableCAMSUserProfileValidation) return [3 /*break*/, 3];
450
476
  email_1 = ((_a = response.account) === null || _a === void 0 ? void 0 : _a.username) || ((_b = response.idTokenClaims) === null || _b === void 0 ? void 0 : _b.preferred_username) || "";
477
+ localStorage.setItem(storageKey, JSON.stringify({ accessToken: response.accessToken, idToken: response.idToken, email: email_1, appCode: appCode }));
478
+ if (!DisableCAMSUserProfileValidation) return [3 /*break*/, 3];
451
479
  setAccessToken(response.accessToken);
452
480
  setIdToken(response.idToken);
453
481
  setEmail(email_1);
@@ -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);
@@ -1728,13 +1759,13 @@ var DefaultLoginPage = function (_a) {
1728
1759
  onADLoginSuccess === null || onADLoginSuccess === void 0 ? void 0 : onADLoginSuccess();
1729
1760
  } }) }) }));
1730
1761
  }
1731
- return (jsxRuntime.jsxs("main", { className: "cams-sdk min-h-screen bg-gray-50", children: [jsxRuntime.jsx(framerMotion.motion.div, { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 }, transition: { duration: 0.5 }, children: jsxRuntime.jsx("div", { className: "flex h-screen items-center justify-center", children: jsxRuntime.jsxs(framerMotion.motion.div, { variants: cardVariants, initial: "hidden", animate: "visible", exit: "exit", className: "w-full max-w-md p-6 space-y-4 rounded-2xl shadow-2xl", children: [jsxRuntime.jsxs(CardHeader, { className: "text-center space-y-3", children: [jsxRuntime.jsx("div", { className: "w-full flex items-center justify-center", children: jsxRuntime.jsx("img", { src: NIBSSLogo, alt: "NIBSS Logo", width: 225, height: 225 }) }), jsxRuntime.jsx(CardTitle, { className: "text-3xl font-bold", children: "NIBSS CAMS" }), jsxRuntime.jsx(CardTitle, { className: "text-gray-500 dark:text-gray-400 font-bold text-lg", children: "Centralized Authentication" })] }), jsxRuntime.jsxs(CardAction, { className: "w-full flex flex-col items-center justify-center text-center text-gray-500 dark:text-gray-400 mb-8", children: [jsxRuntime.jsx("img", { src: AuthLogo, alt: "Auth Logo", width: 365, height: 365 }), "Use Below Identity Providers To Authenticate"] }), jsxRuntime.jsxs("div", { className: "space-y-4", children: [jsxRuntime.jsxs(Button
1762
+ return (jsxRuntime.jsxs("main", { className: "cams-sdk min-h-screen bg-gray-50", children: [jsxRuntime.jsx(framerMotion.motion.div, { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 }, transition: { duration: 0.5 }, children: jsxRuntime.jsx("div", { className: "flex h-screen items-center justify-center", children: jsxRuntime.jsxs(framerMotion.motion.div, { variants: cardVariants, initial: "hidden", animate: "visible", exit: "exit", className: "w-full max-w-md p-6 space-y-6 rounded-2xl shadow-2xl", children: [jsxRuntime.jsxs(CardHeader, { className: "text-center space-y-3", children: [jsxRuntime.jsx("div", { className: "w-full flex items-center justify-center", children: jsxRuntime.jsx("img", { src: NIBSSLogo, alt: "NIBSS Logo", width: 250, height: 250 }) }), jsxRuntime.jsx(CardTitle, { className: "text-3xl font-bold", children: "NIBSS CAMS" }), jsxRuntime.jsx(CardTitle, { className: "text-gray-500 dark:text-gray-400 font-bold text-2xl", children: "Centralized Authentication" })] }), jsxRuntime.jsxs(CardAction, { className: "w-full flex flex-col items-center justify-center text-center text-gray-500 dark:text-gray-400 mb-8", children: [jsxRuntime.jsx("img", { src: AuthLogo, alt: "Auth Logo", width: 385, height: 385 }), "Use Below Identity Providers To Authenticate"] }), jsxRuntime.jsxs("div", { className: "space-y-6", children: [jsxRuntime.jsxs(Button
1732
1763
  // variant="outline"
1733
1764
  , {
1734
1765
  // variant="outline"
1735
1766
  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: handleMSALLogin, disabled: isLoading, children: [jsxRuntime.jsx("img", { src: MicrosoftLogo, alt: "Microsoft Logo", width: 35, height: 35 }), jsxRuntime.jsx("span", { className: "ml-2", children: isLoading ? "Logging in..." : "Sign in with Microsoft" })] }), useADLogin && (jsxRuntime.jsxs(Button, { 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: function () { return setShowADModal(true); }, disabled: isLoading, children: [jsxRuntime.jsx(lucideReact.KeyIcon, { className: "text-[#506f4a]", size: 64 }), jsxRuntime.jsx("span", { children: isLoading
1736
1767
  ? "Logging in..."
1737
- : "Sign in with ActiveDirectory" })] }))] }), jsxRuntime.jsxs(CardFooter, { className: "flex items-center justify-center mt-6 space-x-2 text-gray-400 text-sm", children: [jsxRuntime.jsx(lucideReact.ShieldCheck, { className: "w-4 h-4 text-[#506f4a] pulse-glow" }), jsxRuntime.jsx("span", { children: "Powered By NIBSS" })] })] }) }) }, "landing"), jsxRuntime.jsx(ADLoginModal, { open: showADModal, onOpenChange: setShowADModal, isLoading: context.isLoading || isCredAuthLoading, setIsLoading: setIsCredAuthLoading, onLogin: function (_a) { return __awaiter$1(void 0, [_a], void 0, function (_b) {
1768
+ : "Sign in with Active Directory" })] }))] }), jsxRuntime.jsxs(CardFooter, { className: "flex items-center justify-center mt-6 space-x-2 text-gray-400 text-sm", children: [jsxRuntime.jsx(lucideReact.ShieldCheck, { className: "w-6 h-6 text-[#506f4a] pulse-glow" }), jsxRuntime.jsx("span", { className: "font-semibold", children: "Powered By NIBSS" })] })] }) }) }, "landing"), jsxRuntime.jsx(ADLoginModal, { open: showADModal, onOpenChange: setShowADModal, isLoading: context.isLoading || isCredAuthLoading, setIsLoading: setIsCredAuthLoading, onLogin: function (_a) { return __awaiter$1(void 0, [_a], void 0, function (_b) {
1738
1769
  var username = _b.username, password = _b.password, MFACode = _b.MFACode, appCode = _b.appCode;
1739
1770
  return __generator$1(this, function (_c) {
1740
1771
  switch (_c.label) {