@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.esm.js CHANGED
@@ -394,15 +394,42 @@ function useCAMSMSALAuth(options) {
394
394
  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;
395
395
  var storageKey = optStorageKey || "CAMS-MSAL-AUTH-SDK";
396
396
  var _c = useMsal(), instance = _c.instance, inProgress = _c.inProgress, accounts = _c.accounts;
397
- var account = useAccount(accounts[0] || {});
397
+ useAccount(accounts[0] || {});
398
+ var isTokenValid = function (token) {
399
+ try {
400
+ var payload = JSON.parse(atob(token.split(".")[1]));
401
+ return payload.exp * 1000 > Date.now();
402
+ }
403
+ catch (_a) {
404
+ return false;
405
+ }
406
+ };
407
+ var readFromStorage = function () {
408
+ var _a, _b, _c;
409
+ if (typeof window === "undefined")
410
+ return { accessToken: "", idToken: "", email: "" };
411
+ try {
412
+ var raw = localStorage.getItem(storageKey);
413
+ if (!raw)
414
+ return { accessToken: "", idToken: "", email: "" };
415
+ var parsed = JSON.parse(raw);
416
+ var token = (_a = parsed.accessToken) !== null && _a !== void 0 ? _a : "";
417
+ return isTokenValid(token)
418
+ ? { accessToken: token, idToken: (_b = parsed.idToken) !== null && _b !== void 0 ? _b : "", email: (_c = parsed.email) !== null && _c !== void 0 ? _c : "" }
419
+ : { accessToken: "", idToken: "", email: "" };
420
+ }
421
+ catch (_d) {
422
+ return { accessToken: "", idToken: "", email: "" };
423
+ }
424
+ };
398
425
  var _d = useState(null), error = _d[0], setError = _d[1];
399
- var _e = useState(""), idToken = _e[0], setIdToken = _e[1];
400
- var _f = useState(""), accessToken = _f[0], setAccessToken = _f[1];
401
- var _g = useState(""), email = _g[0], setEmail = _g[1];
426
+ var _e = useState(function () { return readFromStorage().idToken; }), idToken = _e[0], setIdToken = _e[1];
427
+ var _f = useState(function () { return readFromStorage().accessToken; }), accessToken = _f[0], setAccessToken = _f[1];
428
+ var _g = useState(function () { return readFromStorage().email; }), email = _g[0], setEmail = _g[1];
402
429
  var _h = useState(false), requiresMFA = _h[0], setRequiresMFA = _h[1];
403
430
  var _j = useState(false), isFetchingMFAConfig = _j[0], setIsFetchingMFAConfig = _j[1];
404
431
  var isLoading = inProgress !== InteractionStatus.None || isFetchingMFAConfig;
405
- var isAuthenticated = !!account && !!accessToken && !requiresMFA;
432
+ var isAuthenticated = !!accessToken && !requiresMFA;
406
433
  var scopes = optScopes || ["openid", "profile", "email"];
407
434
  var login = useCallback(function () { return __awaiter$1(_this, void 0, void 0, function () {
408
435
  var response, authenticator, email_1, userConfig, camsError, error_1, err_1, camsError_1, camsError;
@@ -425,8 +452,9 @@ function useCAMSMSALAuth(options) {
425
452
  case 2:
426
453
  response = _e.sent();
427
454
  authenticator = new CAMSMFAAuthenticator();
428
- if (!DisableCAMSUserProfileValidation) return [3 /*break*/, 3];
429
455
  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) || "";
456
+ localStorage.setItem(storageKey, JSON.stringify({ accessToken: response.accessToken, idToken: response.idToken, email: email_1, appCode: appCode }));
457
+ if (!DisableCAMSUserProfileValidation) return [3 /*break*/, 3];
430
458
  setAccessToken(response.accessToken);
431
459
  setIdToken(response.idToken);
432
460
  setEmail(email_1);
@@ -457,6 +485,7 @@ function useCAMSMSALAuth(options) {
457
485
  setIdToken(response.idToken);
458
486
  setEmail(userConfig.userInfo.email);
459
487
  setRequiresMFA(userConfig.userInfo.isMFAEnabled);
488
+ localStorage.setItem(storageKey, JSON.stringify({ accessToken: response.accessToken, idToken: response.idToken, email: userConfig.userInfo.email, appCode: appCode }));
460
489
  }
461
490
  else {
462
491
  camsError = new CAMSError(CAMSErrorType.API_VALIDATION_ERROR, "Login failed: " + userConfig.message);
@@ -554,6 +583,8 @@ function useCAMSMSALAuth(options) {
554
583
  setEmail("");
555
584
  setError(null);
556
585
  setRequiresMFA(false);
586
+ if (typeof window !== "undefined")
587
+ localStorage.removeItem(storageKey);
557
588
  }
558
589
  catch (err) {
559
590
  camsError = new CAMSError(CAMSErrorType.API_VALIDATION_ERROR, "Logout failed: " + err);
@@ -1707,13 +1738,13 @@ var DefaultLoginPage = function (_a) {
1707
1738
  onADLoginSuccess === null || onADLoginSuccess === void 0 ? void 0 : onADLoginSuccess();
1708
1739
  } }) }) }));
1709
1740
  }
1710
- return (jsxs("main", { className: "cams-sdk min-h-screen bg-gray-50", children: [jsx(motion.div, { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 }, transition: { duration: 0.5 }, children: jsx("div", { className: "flex h-screen items-center justify-center", children: jsxs(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: [jsxs(CardHeader, { className: "text-center space-y-3", children: [jsx("div", { className: "w-full flex items-center justify-center", children: jsx("img", { src: NIBSSLogo, alt: "NIBSS Logo", width: 225, height: 225 }) }), jsx(CardTitle, { className: "text-3xl font-bold", children: "NIBSS CAMS" }), jsx(CardTitle, { className: "text-gray-500 dark:text-gray-400 font-bold text-lg", children: "Centralized Authentication" })] }), jsxs(CardAction, { className: "w-full flex flex-col items-center justify-center text-center text-gray-500 dark:text-gray-400 mb-8", children: [jsx("img", { src: AuthLogo, alt: "Auth Logo", width: 365, height: 365 }), "Use Below Identity Providers To Authenticate"] }), jsxs("div", { className: "space-y-4", children: [jsxs(Button
1741
+ return (jsxs("main", { className: "cams-sdk min-h-screen bg-gray-50", children: [jsx(motion.div, { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 }, transition: { duration: 0.5 }, children: jsx("div", { className: "flex h-screen items-center justify-center", children: jsxs(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: [jsxs(CardHeader, { className: "text-center space-y-3", children: [jsx("div", { className: "w-full flex items-center justify-center", children: jsx("img", { src: NIBSSLogo, alt: "NIBSS Logo", width: 250, height: 250 }) }), jsx(CardTitle, { className: "text-3xl font-bold", children: "NIBSS CAMS" }), jsx(CardTitle, { className: "text-gray-500 dark:text-gray-400 font-bold text-2xl", children: "Centralized Authentication" })] }), jsxs(CardAction, { className: "w-full flex flex-col items-center justify-center text-center text-gray-500 dark:text-gray-400 mb-8", children: [jsx("img", { src: AuthLogo, alt: "Auth Logo", width: 385, height: 385 }), "Use Below Identity Providers To Authenticate"] }), jsxs("div", { className: "space-y-6", children: [jsxs(Button
1711
1742
  // variant="outline"
1712
1743
  , {
1713
1744
  // variant="outline"
1714
1745
  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: [jsx("img", { src: MicrosoftLogo, alt: "Microsoft Logo", width: 35, height: 35 }), jsx("span", { className: "ml-2", children: isLoading ? "Logging in..." : "Sign in with Microsoft" })] }), useADLogin && (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: [jsx(KeyIcon, { className: "text-[#506f4a]", size: 64 }), jsx("span", { children: isLoading
1715
1746
  ? "Logging in..."
1716
- : "Sign in with ActiveDirectory" })] }))] }), jsxs(CardFooter, { className: "flex items-center justify-center mt-6 space-x-2 text-gray-400 text-sm", children: [jsx(ShieldCheck, { className: "w-4 h-4 text-[#506f4a] pulse-glow" }), jsx("span", { children: "Powered By NIBSS" })] })] }) }) }, "landing"), 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) {
1747
+ : "Sign in with Active Directory" })] }))] }), jsxs(CardFooter, { className: "flex items-center justify-center mt-6 space-x-2 text-gray-400 text-sm", children: [jsx(ShieldCheck, { className: "w-6 h-6 text-[#506f4a] pulse-glow" }), jsx("span", { className: "font-semibold", children: "Powered By NIBSS" })] })] }) }) }, "landing"), 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) {
1717
1748
  var username = _b.username, password = _b.password, MFACode = _b.MFACode, appCode = _b.appCode;
1718
1749
  return __generator$1(this, function (_c) {
1719
1750
  switch (_c.label) {