@nibssplc/cams-sdk-react 0.0.1-beta.105 → 0.0.1-beta.106

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.
@@ -1,7 +1,9 @@
1
1
  interface ADLoginModalProps {
2
2
  open: boolean;
3
+ isLoading: boolean;
4
+ setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;
3
5
  onOpenChange: (open: boolean) => void;
4
6
  onLogin: (credentials: Credentials) => Promise<void>;
5
7
  }
6
- export declare const ADLoginModal: ({ open, onOpenChange, onLogin, }: ADLoginModalProps) => import("react/jsx-runtime").JSX.Element;
8
+ export declare const ADLoginModal: ({ open, isLoading, setIsLoading, onOpenChange, onLogin, }: ADLoginModalProps) => import("react/jsx-runtime").JSX.Element;
7
9
  export {};
@@ -15,13 +15,8 @@ export declare const useOTPHandler: ({ provider, accessToken, idToken, appCode,
15
15
  resetAttempts: () => void;
16
16
  remainingAttempts: number;
17
17
  };
18
- export declare const CredentialsHandler: (credentials: Credentials, appCode: string, CredAuthEndpoint: string, onAuthComplete: (state: boolean, data: UserValidatedResponse | null) => void) => {
19
- handleSubmitCredentials: ({ username, password, MFACode, appCode, }: {
20
- username: string;
21
- password: string;
22
- MFACode: string;
23
- appCode: string;
24
- }) => Promise<boolean>;
18
+ export declare const useCredentialsHandler: (onAuthComplete: (state: boolean, data: UserValidatedResponse | null) => void) => {
19
+ handleSubmitCredentials: (CredAuthEndpoint: string, credentials: Credentials, appCode: string) => Promise<boolean>;
25
20
  loading: boolean;
26
21
  setLoading: import("react").Dispatch<import("react").SetStateAction<boolean>>;
27
22
  attemptCount: number;
package/dist/index.cjs.js CHANGED
@@ -1740,7 +1740,7 @@ var GenerateDeviceId = function () {
1740
1740
  var userAgent = window.navigator.userAgent;
1741
1741
  var deviceId = "";
1742
1742
  // Parse browser, version, OS, and device type
1743
- var browserMatch = Array.from(userAgent.matchAll(/(Chrome|Firefox|Safari|Edge|Opera)\/([\d.]+)/ig));
1743
+ var browserMatch = Array.from(userAgent.matchAll(/(Chrome|Firefox|Safari|Edge|Opera)\/([\d.]+)/gi));
1744
1744
  var osMatch = Array.from(userAgent.matchAll(/\(([^)]+)\)/g));
1745
1745
  var isMobile = /Mobile|Android|iPhone|iPad/i.test(userAgent);
1746
1746
  // Browser info
@@ -1770,6 +1770,10 @@ var GenerateDeviceId = function () {
1770
1770
  deviceId += "_".concat(hash);
1771
1771
  return (_a = deviceId.replace(/[^a-zA-Z0-9-_]/g, "_")) !== null && _a !== void 0 ? _a : "unknown-device";
1772
1772
  };
1773
+ var APIHeaders = {
1774
+ "X-DEVICE-ID": GenerateDeviceId(),
1775
+ "X-API-VERSION": "1.0",
1776
+ };
1773
1777
 
1774
1778
  var MAX_ATTEMPTS = 3;
1775
1779
  var useOTPHandler = function (_a) {
@@ -1804,9 +1808,7 @@ var useOTPHandler = function (_a) {
1804
1808
  MFACode: authenticationValue,
1805
1809
  appCode: appCode,
1806
1810
  }, {
1807
- headers: {
1808
- "X-Device-ID": GenerateDeviceId(),
1809
- },
1811
+ headers: APIHeaders,
1810
1812
  timeout: 605000,
1811
1813
  })];
1812
1814
  case 2:
@@ -1869,34 +1871,31 @@ var useOTPHandler = function (_a) {
1869
1871
  remainingAttempts: MAX_ATTEMPTS - attemptCount,
1870
1872
  };
1871
1873
  };
1872
- var CredentialsHandler = function (credentials, appCode, CredAuthEndpoint, onAuthComplete) {
1874
+ var useCredentialsHandler = function (onAuthComplete) {
1873
1875
  var _a = React.useState(false), loading = _a[0], setLoading = _a[1];
1874
1876
  var _b = React.useState(0), attemptCount = _b[0], setAttemptCount = _b[1];
1875
1877
  var _c = React.useState(false), isMaxAttemptsReached = _c[0], setIsMaxAttemptsReached = _c[1];
1876
1878
  var handleSubmitCredentials = React.useMemo(function () {
1877
- return function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
1879
+ return function (CredAuthEndpoint, credentials, appCode) { return __awaiter(void 0, void 0, void 0, function () {
1878
1880
  var currentAttempt, response, error_2, currentAttempt;
1879
- var username = _b.username, password = _b.password, MFACode = _b.MFACode, appCode = _b.appCode;
1880
- return __generator(this, function (_c) {
1881
- switch (_c.label) {
1881
+ return __generator(this, function (_a) {
1882
+ switch (_a.label) {
1882
1883
  case 0:
1883
- _c.trys.push([0, 2, 3, 4]);
1884
+ _a.trys.push([0, 2, 3, 4]);
1884
1885
  setLoading(true);
1885
1886
  currentAttempt = attemptCount + 1;
1886
1887
  setAttemptCount(currentAttempt);
1887
1888
  return [4 /*yield*/, axios.post(CredAuthEndpoint, {
1888
- username: username,
1889
- password: password,
1890
- MFACode: MFACode,
1889
+ username: credentials.username,
1890
+ password: credentials.password,
1891
+ MFACode: credentials.MFACode,
1891
1892
  appCode: appCode,
1892
1893
  }, {
1893
- headers: {
1894
- "X-Device-ID": GenerateDeviceId(),
1895
- },
1894
+ headers: APIHeaders,
1896
1895
  timeout: 605000,
1897
1896
  })];
1898
1897
  case 1:
1899
- response = (_c.sent()).data;
1898
+ response = (_a.sent()).data;
1900
1899
  if (response) {
1901
1900
  onAuthComplete(true, response);
1902
1901
  return [2 /*return*/, true];
@@ -1914,7 +1913,7 @@ var CredentialsHandler = function (credentials, appCode, CredAuthEndpoint, onAut
1914
1913
  return [2 /*return*/, false];
1915
1914
  }
1916
1915
  case 2:
1917
- error_2 = _c.sent();
1916
+ error_2 = _a.sent();
1918
1917
  console.error(error_2);
1919
1918
  currentAttempt = attemptCount + 1;
1920
1919
  if (currentAttempt >= MAX_ATTEMPTS) {
@@ -1933,7 +1932,7 @@ var CredentialsHandler = function (credentials, appCode, CredAuthEndpoint, onAut
1933
1932
  }
1934
1933
  });
1935
1934
  }); };
1936
- }, [credentials.username, credentials.password, credentials.MFACode]);
1935
+ }, []);
1937
1936
  return {
1938
1937
  handleSubmitCredentials: handleSubmitCredentials,
1939
1938
  loading: loading,
@@ -1969,10 +1968,7 @@ var MFAOptions = function (_a) {
1969
1968
  // 1. Fetch authentication challenge from your server
1970
1969
  console.log("Requesting authentication challenge from server...");
1971
1970
  return [4 /*yield*/, axios.post(MFAEndpoints.RetrieveAuthChallenge, {}, {
1972
- headers: {
1973
- "X-DEVICE-ID": GenerateDeviceId(),
1974
- "X-API-VERSION": "1.0",
1975
- },
1971
+ headers: APIHeaders,
1976
1972
  })];
1977
1973
  case 1:
1978
1974
  options = (_a.sent()).data;
@@ -1986,10 +1982,7 @@ var MFAOptions = function (_a) {
1986
1982
  // 3. Send the assertion back to the server for verification
1987
1983
  console.log("Sending assertion to server for verification...");
1988
1984
  return [4 /*yield*/, axios.post(MFAEndpoints.AuthChallengeVerify, assertionResponse, {
1989
- headers: {
1990
- "X-DEVICE-ID": GenerateDeviceId(),
1991
- "X-API-VERSION": "1.0",
1992
- },
1985
+ headers: APIHeaders,
1993
1986
  withCredentials: true, // credentials: 'include'
1994
1987
  })];
1995
1988
  case 3:
@@ -2116,14 +2109,13 @@ var credentialsSchema = z.z.object({
2116
2109
  password: z.z.string().min(1, "Password is required"),
2117
2110
  });
2118
2111
  var ADLoginModal = function (_a) {
2119
- var open = _a.open, onOpenChange = _a.onOpenChange, onLogin = _a.onLogin;
2112
+ var open = _a.open, isLoading = _a.isLoading, setIsLoading = _a.setIsLoading, onOpenChange = _a.onOpenChange, onLogin = _a.onLogin;
2120
2113
  var _b = React.useState("credentials"), step = _b[0], setStep = _b[1];
2121
2114
  var _c = React.useState({
2122
2115
  username: "",
2123
2116
  password: "",
2124
2117
  }), credentials = _c[0], setCredentials = _c[1];
2125
2118
  var _d = React.useState(""), mfaCode = _d[0], setMfaCode = _d[1];
2126
- var _e = React.useState(false), isLoading = _e[0], setIsLoading = _e[1];
2127
2119
  var form = reactHookForm.useForm({
2128
2120
  resolver: a$1(credentialsSchema),
2129
2121
  defaultValues: { username: "", password: "" },
@@ -2173,7 +2165,7 @@ var ADLoginModal = function (_a) {
2173
2165
  form.reset();
2174
2166
  setMfaCode("");
2175
2167
  };
2176
- return (jsxRuntimeExports.jsx(Dialog, { open: open, onOpenChange: handleClose, children: jsxRuntimeExports.jsxs(DialogContent, { className: "sm:max-w-[75%] max-w-[400px]", children: [jsxRuntimeExports.jsx(DialogHeader, { children: jsxRuntimeExports.jsxs("div", { className: "flex items-center gap-2", children: [jsxRuntimeExports.jsx(lucideReact.KeyIcon, { className: "w-8 h-8 text-[#506f4a]" }), jsxRuntimeExports.jsx(DialogTitle, { className: "text-2xl", children: "Sign in with AD" })] }) }), step === "credentials" ? (jsxRuntimeExports.jsx(Form, __assign({}, form, { children: jsxRuntimeExports.jsxs("form", { onSubmit: form.handleSubmit(handleCredentialsSubmit), className: "space-y-4", children: [jsxRuntimeExports.jsx(FormField, { control: form.control, name: "username", render: function (_a) {
2168
+ return (jsxRuntimeExports.jsx(Dialog, { open: open, onOpenChange: handleClose, children: jsxRuntimeExports.jsxs(DialogContent, { className: "w-[70vw] min-w-[250px] max-w-[350px]", children: [jsxRuntimeExports.jsx(DialogHeader, { children: jsxRuntimeExports.jsxs("div", { className: "flex items-center gap-2", children: [jsxRuntimeExports.jsx(lucideReact.KeyIcon, { className: "w-8 h-8 text-[#506f4a]" }), jsxRuntimeExports.jsx(DialogTitle, { className: "text-2xl", children: "Sign in with AD" })] }) }), step === "credentials" ? (jsxRuntimeExports.jsx(Form, __assign({}, form, { children: jsxRuntimeExports.jsxs("form", { onSubmit: form.handleSubmit(handleCredentialsSubmit), className: "space-y-4", children: [jsxRuntimeExports.jsx(FormField, { control: form.control, name: "username", render: function (_a) {
2177
2169
  var field = _a.field;
2178
2170
  return (jsxRuntimeExports.jsxs(FormItem, { children: [jsxRuntimeExports.jsx(FormLabel, { children: "Username" }), jsxRuntimeExports.jsx(FormControl, { children: jsxRuntimeExports.jsx(Input, __assign({ className: "h-12", placeholder: "Enter your username" }, field)) }), jsxRuntimeExports.jsx(FormMessage, {})] }));
2179
2171
  } }), jsxRuntimeExports.jsx(FormField, { control: form.control, name: "password", render: function (_a) {
@@ -2198,6 +2190,20 @@ var DefaultLoginPage = function (_a) {
2198
2190
  var login = context.login, isLoading = context.isLoading, authMode = context.authMode;
2199
2191
  var _b = React.useState(false), showADModal = _b[0], setShowADModal = _b[1];
2200
2192
  var register = useWebAuthn().register;
2193
+ var _c = useCredentialsHandler(function (state, data) { return __awaiter(void 0, void 0, void 0, function () {
2194
+ return __generator(this, function (_a) {
2195
+ console.log(data);
2196
+ if (state && data) {
2197
+ context.isAuthenticated = true;
2198
+ context.requiresMFA = false;
2199
+ context.setUserProfile({
2200
+ type: "AUTH_SUCCESS",
2201
+ userProfile: __assign({}, data),
2202
+ });
2203
+ }
2204
+ return [2 /*return*/];
2205
+ });
2206
+ }); }), handleSubmitCredentials = _c.handleSubmitCredentials, isCredAuthLoading = _c.loading, setIsCredAuthLoading = _c.setLoading;
2201
2207
  var handleMSALLogin = function () {
2202
2208
  if (authMode === "MSAL") {
2203
2209
  login();
@@ -2216,10 +2222,7 @@ var DefaultLoginPage = function (_a) {
2216
2222
  // 1. Fetch challenge from your server
2217
2223
  console.log("Requesting registration challenge from server...");
2218
2224
  return [4 /*yield*/, axios.post(MFAEndpoints.RegisterNewChallenge, __assign({}, data), {
2219
- headers: {
2220
- "X-DEVICE-ID": GenerateDeviceId(),
2221
- "X-API-VERSION": "1.0",
2222
- },
2225
+ headers: APIHeaders,
2223
2226
  })];
2224
2227
  case 1:
2225
2228
  options = (_a.sent()).data;
@@ -2233,10 +2236,7 @@ var DefaultLoginPage = function (_a) {
2233
2236
  // 3. Send the response back to the server for verification
2234
2237
  console.log("Sending attestation to server for verification...");
2235
2238
  return [4 /*yield*/, axios.post(MFAEndpoints.RegisterVerify, attestationResponse, {
2236
- headers: {
2237
- "X-DEVICE-ID": GenerateDeviceId(),
2238
- "X-API-VERSION": "1.0",
2239
- },
2239
+ headers: APIHeaders,
2240
2240
  withCredentials: true, // credentials: 'include'
2241
2241
  })];
2242
2242
  case 3:
@@ -2256,28 +2256,15 @@ var DefaultLoginPage = function (_a) {
2256
2256
  // variant="outline"
2257
2257
  , {
2258
2258
  // variant="outline"
2259
- 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: [jsxRuntimeExports.jsx("img", { src: MicrosoftLogo, alt: "Microsoft Logo", width: 35, height: 35 }), jsxRuntimeExports.jsx("span", { className: "ml-2", children: isLoading ? "Logging in..." : "Sign in with Microsoft" })] }), useADLogin && (jsxRuntimeExports.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: [jsxRuntimeExports.jsx(lucideReact.KeyIcon, { className: "w-24 h-24 text-[#506f4a]" }), jsxRuntimeExports.jsx("span", { children: isLoading
2259
+ 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: [jsxRuntimeExports.jsx("img", { src: MicrosoftLogo, alt: "Microsoft Logo", width: 35, height: 35 }), jsxRuntimeExports.jsx("span", { className: "ml-2", children: isLoading ? "Logging in..." : "Sign in with Microsoft" })] }), useADLogin && (jsxRuntimeExports.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: [jsxRuntimeExports.jsx(lucideReact.KeyIcon, { className: "text-[#506f4a]", size: 64 }), jsxRuntimeExports.jsx("span", { children: isLoading
2260
2260
  ? "Logging in..."
2261
- : "Sign in with ActiveDirectory" })] })), usePassKey && (jsxRuntimeExports.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 handleRegister(PassKeysRegisterProps); }, disabled: isLoading, children: [jsxRuntimeExports.jsx(lucideReact.KeyIcon, { className: "w-12 h-12 text-[#506f4a]" }), jsxRuntimeExports.jsx("span", { children: "Create a Passkey" })] }))] }), jsxRuntimeExports.jsxs(CardFooter, { className: "flex items-center justify-center mt-6 space-x-2 text-gray-400 text-sm", children: [jsxRuntimeExports.jsx(lucideReact.ShieldCheck, { className: "w-4 h-4 text-[#506f4a] pulse-glow" }), jsxRuntimeExports.jsx("span", { children: "Powered By NIBSS" })] })] }) }) }, "landing"), jsxRuntimeExports.jsx(ADLoginModal, { open: showADModal, onOpenChange: setShowADModal, onLogin: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
2261
+ : "Sign in with ActiveDirectory" })] })), usePassKey && (jsxRuntimeExports.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 handleRegister(PassKeysRegisterProps); }, disabled: isLoading, children: [jsxRuntimeExports.jsx(lucideReact.KeyIcon, { className: "text-[#506f4a]", size: 64 }), jsxRuntimeExports.jsx("span", { children: "Create a Passkey" })] }))] }), jsxRuntimeExports.jsxs(CardFooter, { className: "flex items-center justify-center mt-6 space-x-2 text-gray-400 text-sm", children: [jsxRuntimeExports.jsx(lucideReact.ShieldCheck, { className: "w-4 h-4 text-[#506f4a] pulse-glow" }), jsxRuntimeExports.jsx("span", { children: "Powered By NIBSS" })] })] }) }) }, "landing"), jsxRuntimeExports.jsx(ADLoginModal, { open: showADModal, onOpenChange: setShowADModal, isLoading: isCredAuthLoading, setIsLoading: setIsCredAuthLoading, onLogin: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
2262
2262
  var username = _b.username, password = _b.password, MFACode = _b.MFACode;
2263
2263
  return __generator(this, function (_c) {
2264
2264
  // Implement your AD login logic here
2265
2265
  console.log("AD Login:", { username: username, password: password, MFACode: MFACode });
2266
2266
  // Example: await adLoginService(username, password, mfaCode);
2267
- CredentialsHandler({ username: username, password: password, MFACode: MFACode }, context.appCode, CredentialsAuthEndpoint !== null && CredentialsAuthEndpoint !== void 0 ? CredentialsAuthEndpoint : "/api/auth/validate", function (state, data) { return __awaiter(void 0, void 0, void 0, function () {
2268
- return __generator(this, function (_a) {
2269
- console.log(data);
2270
- if (state && data) {
2271
- context.isAuthenticated = true;
2272
- context.requiresMFA = false;
2273
- context.setUserProfile({
2274
- type: "AUTH_SUCCESS",
2275
- userProfile: __assign({}, data),
2276
- });
2277
- }
2278
- return [2 /*return*/];
2279
- });
2280
- }); });
2267
+ handleSubmitCredentials(CredentialsAuthEndpoint !== null && CredentialsAuthEndpoint !== void 0 ? CredentialsAuthEndpoint : "/api/auth/validate", { username: username, password: password, MFACode: MFACode }, context.appCode);
2281
2268
  return [2 /*return*/];
2282
2269
  });
2283
2270
  }); } })] }));