@nibssplc/cams-sdk-react 1.0.0-rc.82 → 1.0.0-rc.83

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
@@ -8,7 +8,7 @@ import { toast } from 'sonner';
8
8
  import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
9
9
  import z$1, { z } from 'zod';
10
10
  import { OTPInput, OTPInputContext, REGEXP_ONLY_DIGITS } from 'input-otp';
11
- import { RectangleEllipsis, XIcon, CheckCircle, Shield, KeyIcon, ShieldCheck, ShieldClose, Loader2 } from 'lucide-react';
11
+ import { RectangleEllipsis, XIcon, CheckCircle, Shield, KeyIcon, ShieldCheck, ShieldClose, EyeOff, Eye, Loader2 } from 'lucide-react';
12
12
  import { appendErrors, FormProvider, Controller, useFormContext, useFormState, useForm } from 'react-hook-form';
13
13
  import { validateFieldsNatively, toNestErrors } from '@hookform/resolvers';
14
14
  import { clsx } from 'clsx';
@@ -420,11 +420,9 @@ function useCAMSMSALAuth(options) {
420
420
  var _d = useState(""), idToken = _d[0], setIdToken = _d[1];
421
421
  var _e = useState(""), accessToken = _e[0], setAccessToken = _e[1];
422
422
  var _f = useState(""), email = _f[0], setEmail = _f[1];
423
- // const [mfaAuthenticator, setMfaAuthenticator] =
424
- // useState<CAMSMFAAuthenticator | null>(null);
425
423
  var _g = useState(false), requiresMFA = _g[0], setRequiresMFA = _g[1];
426
424
  var isLoading = inProgress !== InteractionStatus.None;
427
- var isAuthenticated = !!account && !!accessToken && !requiresMFA;
425
+ var isAuthenticated = !!account && !!accessToken;
428
426
  var scopes = optScopes || ["openid", "profile", "email"];
429
427
  var isTokenValid = function (token) {
430
428
  try {
@@ -1642,12 +1640,13 @@ var credentialsSchema = z.object({
1642
1640
  });
1643
1641
  var ADLoginModal = function (_a) {
1644
1642
  var open = _a.open, isLoading = _a.isLoading, setIsLoading = _a.setIsLoading, onOpenChange = _a.onOpenChange, onLogin = _a.onLogin;
1645
- var _b = useState("credentials"), step = _b[0], setStep = _b[1];
1646
- var _c = useState({
1643
+ var _b = useState("Credentials"), step = _b[0], setStep = _b[1];
1644
+ var _c = useState(false), showPassword = _c[0], setShowPassword = _c[1];
1645
+ var _d = useState({
1647
1646
  username: "",
1648
1647
  password: "",
1649
- }), credentials = _c[0], setCredentials = _c[1];
1650
- var _d = useState(""), mfaCode = _d[0], setMfaCode = _d[1];
1648
+ }), credentials = _d[0], setCredentials = _d[1];
1649
+ var _e = useState(""), MFACode = _e[0], setMFACode = _e[1];
1651
1650
  var form = useForm({
1652
1651
  resolver: a$1(credentialsSchema),
1653
1652
  defaultValues: { username: "", password: "" },
@@ -1655,7 +1654,7 @@ var ADLoginModal = function (_a) {
1655
1654
  var handleCredentialsSubmit = function (values) { return __awaiter$1(void 0, void 0, void 0, function () {
1656
1655
  return __generator$1(this, function (_a) {
1657
1656
  setCredentials(values);
1658
- setStep("mfa");
1657
+ setStep("MFA");
1659
1658
  return [2 /*return*/];
1660
1659
  });
1661
1660
  }); };
@@ -1677,9 +1676,9 @@ var ADLoginModal = function (_a) {
1677
1676
  case 2:
1678
1677
  _a.sent();
1679
1678
  onOpenChange(false);
1680
- setStep("credentials");
1679
+ setStep("Credentials");
1681
1680
  form.reset();
1682
- setMfaCode("");
1681
+ setMFACode("");
1683
1682
  return [3 /*break*/, 5];
1684
1683
  case 3:
1685
1684
  error_1 = _a.sent();
@@ -1694,17 +1693,18 @@ var ADLoginModal = function (_a) {
1694
1693
  }); };
1695
1694
  var handleClose = function () {
1696
1695
  onOpenChange(false);
1697
- setStep("credentials");
1696
+ setStep("Credentials");
1698
1697
  form.reset();
1699
- setMfaCode("");
1698
+ setMFACode("");
1699
+ setShowPassword(false);
1700
1700
  };
1701
- return (jsx(Dialog, { open: open, onOpenChange: handleClose, children: jsxs(DialogContent, { className: "min-w-[50vw] max-w-[70vw]", children: [jsx(DialogHeader, { children: jsxs("div", { className: "flex items-center gap-4", children: [jsx(KeyIcon, { className: "w-8 h-8 text-[#506f4a]" }), jsx(DialogTitle, { className: "text-xl", children: "Sign in with AD Credentials" })] }) }), step === "credentials" ? (jsx(Form, __assign({}, form, { children: jsxs("form", { onSubmit: form.handleSubmit(handleCredentialsSubmit), className: "space-y-6", children: [jsx(FormField, { control: form.control, name: "username", render: function (_a) {
1701
+ return (jsx(Dialog, { open: open, onOpenChange: handleClose, children: jsxs(DialogContent, { className: "min-w-[50vw] max-w-[70vw]", children: [jsx(DialogHeader, { children: jsxs("div", { className: "flex items-center gap-4", children: [jsx(KeyIcon, { className: "w-8 h-8 text-[#506f4a]" }), jsx(DialogTitle, { className: "text-xl", children: "Sign in with AD Credentials" })] }) }), step === "Credentials" ? (jsx(Form, __assign({}, form, { children: jsxs("form", { onSubmit: form.handleSubmit(handleCredentialsSubmit), className: "space-y-6", children: [jsx(FormField, { control: form.control, name: "username", render: function (_a) {
1702
1702
  var field = _a.field;
1703
1703
  return (jsxs(FormItem, { children: [jsx(FormLabel, { children: "Username" }), jsx(FormControl, { children: jsx(Input, __assign({ className: "h-12", placeholder: "Enter your username" }, field)) }), jsx(FormMessage, {})] }));
1704
1704
  } }), jsx(FormField, { control: form.control, name: "password", render: function (_a) {
1705
1705
  var field = _a.field;
1706
- return (jsxs(FormItem, { children: [jsx(FormLabel, { children: "Password" }), jsx(FormControl, { children: jsx(Input, __assign({ className: "h-12", type: "password", placeholder: "Enter your password" }, field)) }), jsx(FormMessage, {})] }));
1707
- } }), jsx(Button, { type: "submit", className: "py-3 w-full bg-[#506f4a] hover:bg-[#506f4a]/90", children: "Continue" })] }) }))) : (jsxs("div", { className: "space-y-4", children: [jsx(GenericOTPVerifier, { value: mfaCode, setValue: setMfaCode, setLoading: setIsLoading, isDisabled: isLoading, onChangeOTP: handleMFASubmit, fieldName: "AuthenticatorCode" }), isLoading && (jsxs("div", { className: "flex items-center justify-center gap-2 text-sm text-muted-foreground", children: [jsx(Loader2, { className: "w-4 h-4 animate-spin" }), jsx("span", { children: "Verifying..." })] }))] }))] }) }));
1706
+ return (jsxs(FormItem, { children: [jsx(FormLabel, { children: "Password" }), jsx(FormControl, { children: jsxs("div", { className: "relative", children: [jsx(Input, __assign({ className: "h-12 pr-10", type: "password", placeholder: "Enter your password" }, field)), jsx("button", { type: "button", onClick: function () { return setShowPassword(!showPassword); }, className: "absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground", children: showPassword ? (jsx(EyeOff, { className: "w-5 h-5" })) : (jsx(Eye, { className: "w-5 h-5" })) })] }) }), jsx(FormMessage, {})] }));
1707
+ } }), jsx(Button, { type: "submit", className: "py-3 w-full bg-[#506f4a] hover:bg-[#506f4a]/90", children: "Continue" })] }) }))) : (jsxs("div", { className: "space-y-4", children: [jsx(GenericOTPVerifier, { value: MFACode, setValue: setMFACode, setLoading: setIsLoading, isDisabled: isLoading, onChangeOTP: handleMFASubmit, fieldName: "AuthenticatorCode" }), isLoading && (jsxs("div", { className: "flex items-center justify-center gap-2 text-sm text-muted-foreground", children: [jsx(Loader2, { className: "w-4 h-4 animate-spin" }), jsx("span", { children: "Verifying..." })] }))] }))] }) }));
1708
1708
  };
1709
1709
 
1710
1710
  var DefaultLoginPage = function (_a) {
@@ -1729,6 +1729,12 @@ var DefaultLoginPage = function (_a) {
1729
1729
  console.log(data);
1730
1730
  if (state && data && data.isValid) {
1731
1731
  context.isAuthenticated = true;
1732
+ localStorage.setItem(context.storageKey, JSON.stringify({
1733
+ isAuthenticated: false,
1734
+ requiresMFA: false,
1735
+ accessToken: "Credentials",
1736
+ idToken: "Credentials",
1737
+ }));
1732
1738
  context.requiresMFA = false;
1733
1739
  context.setUserProfile({
1734
1740
  state: "AUTH_SUCCESS",
@@ -1744,7 +1750,7 @@ var DefaultLoginPage = function (_a) {
1744
1750
  });
1745
1751
  }
1746
1752
  else {
1747
- toast.error("\u274C AD Login failed. ".concat((data === null || data === void 0 ? void 0 : data.message) || 'Unknown error'));
1753
+ toast.error("\u274C AD Login failed. ".concat((data === null || data === void 0 ? void 0 : data.message) || "Unknown error"));
1748
1754
  }
1749
1755
  return [2 /*return*/];
1750
1756
  });
@@ -1776,7 +1782,12 @@ var DefaultLoginPage = function (_a) {
1776
1782
  return __generator$1(this, function (_c) {
1777
1783
  // Implement your AD login logic here
1778
1784
  console.log("AD Login:", { username: username, MFACode: MFACode });
1779
- handleSubmitCredentials(CredentialsAuthEndpoint, { username: username, password: password, MFACode: MFACode, appCode: context.appCode });
1785
+ handleSubmitCredentials(CredentialsAuthEndpoint, {
1786
+ username: username,
1787
+ password: password,
1788
+ MFACode: MFACode,
1789
+ appCode: context.appCode,
1790
+ });
1780
1791
  return [2 /*return*/];
1781
1792
  });
1782
1793
  }); } })] }));
@@ -1802,7 +1813,7 @@ var MFAGate = function (_a) {
1802
1813
  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;
1803
1814
  var context = useCAMSContext();
1804
1815
  var _e = useState("Loading"), authState = _e[0], setAuthState = _e[1];
1805
- var validatedMFAEndpoints = useMemo(function () {
1816
+ var ValidatedMFAEndpoints = useMemo(function () {
1806
1817
  var parsed = MFAEndpointsSchema.safeParse(MFAEndpoints);
1807
1818
  if (!parsed.success) {
1808
1819
  Logger.error("Invalid MFAEndpoint", parsed.error);
@@ -1860,20 +1871,19 @@ var MFAGate = function (_a) {
1860
1871
  setAuthState("Loading");
1861
1872
  return;
1862
1873
  }
1863
- if (!context.isAuthenticated) {
1874
+ // Check if user has accessToken (MSAL authenticated)
1875
+ if (!contextAccessToken) {
1864
1876
  setAuthState("Unauthenticated");
1865
1877
  return;
1866
1878
  }
1867
- // If authenticated, determine if MFA is required
1879
+ // User has accessToken, determine if MFA is required
1868
1880
  var shouldRequireMFA = requiresMFA !== null && requiresMFA !== void 0 ? requiresMFA : contextRequiresMFA;
1869
1881
  console.log("MFA decision:", {
1870
1882
  shouldRequireMFA: shouldRequireMFA,
1871
1883
  hasAccessToken: !!contextAccessToken,
1872
- finalState: shouldRequireMFA && contextAccessToken
1873
- ? "MFA_Required"
1874
- : "Authenticated",
1884
+ finalState: shouldRequireMFA ? "MFA_Required" : "Authenticated",
1875
1885
  });
1876
- if (shouldRequireMFA && contextAccessToken) {
1886
+ if (shouldRequireMFA) {
1877
1887
  setAuthState("MFA_Required");
1878
1888
  }
1879
1889
  else {
@@ -1885,6 +1895,7 @@ var MFAGate = function (_a) {
1885
1895
  contextRequiresMFA, // Now this will trigger re-evaluation when requiresMFA changes
1886
1896
  contextAccessToken, // And this when accessToken changes
1887
1897
  requiresMFA,
1898
+ context.authMode,
1888
1899
  ]);
1889
1900
  // Debug useEffect
1890
1901
  useEffect(function () {
@@ -1905,7 +1916,7 @@ var MFAGate = function (_a) {
1905
1916
  ]);
1906
1917
  if (useADLogin && !CredentialsAuthEndpoint)
1907
1918
  return jsx(ErrorFallback, { message: "Invalid AD Login Configuration." });
1908
- if (!validatedMFAEndpoints)
1919
+ if (!ValidatedMFAEndpoints)
1909
1920
  return jsx(ErrorFallback, { message: "Invalid MFA Configuration." });
1910
1921
  if (context.authMode !== "MSAL")
1911
1922
  return jsx(Fragment, { children: children });
@@ -1913,12 +1924,12 @@ var MFAGate = function (_a) {
1913
1924
  case "Loading":
1914
1925
  return fallback;
1915
1926
  case "MFA_Required":
1916
- return (jsx(MFAOptions, { MFAEndpoints: validatedMFAEndpoints, usePassKey: usePassKey, onComplete: handleComplete, onAuthFailed: handleAuthFailed }));
1927
+ return (jsx(MFAOptions, { MFAEndpoints: ValidatedMFAEndpoints, usePassKey: usePassKey, onComplete: handleComplete, onAuthFailed: handleAuthFailed }));
1917
1928
  case "Authenticated":
1918
1929
  return jsx(Fragment, { children: children });
1919
1930
  case "Unauthenticated":
1920
1931
  default:
1921
- return (jsx(DefaultLoginPage, { usePassKey: usePassKey, useADLogin: useADLogin, MFAEndpoints: MFAEndpoints, PassKeysRegisterProps: PassKeysRegisterProps, CredentialsAuthEndpoint: CredentialsAuthEndpoint }));
1932
+ return (jsx(DefaultLoginPage, { usePassKey: usePassKey, useADLogin: useADLogin, MFAEndpoints: ValidatedMFAEndpoints, PassKeysRegisterProps: PassKeysRegisterProps, CredentialsAuthEndpoint: CredentialsAuthEndpoint }));
1922
1933
  }
1923
1934
  };
1924
1935