@nibssplc/cams-sdk-react 0.0.1-beta.104 → 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,
@@ -1962,46 +1961,36 @@ var MFAOptions = function (_a) {
1962
1961
  var authenticate = useWebAuthn().authenticate;
1963
1962
  var handleFIDOLogin = function () { return __awaiter(void 0, void 0, void 0, function () {
1964
1963
  var options, assertionResponse, error_1;
1965
- var _a, _b, _c, _d, _e, _f;
1966
- return __generator(this, function (_g) {
1967
- switch (_g.label) {
1964
+ return __generator(this, function (_a) {
1965
+ switch (_a.label) {
1968
1966
  case 0:
1969
- _g.trys.push([0, 4, , 5]);
1967
+ _a.trys.push([0, 4, , 5]);
1970
1968
  // 1. Fetch authentication challenge from your server
1971
1969
  console.log("Requesting authentication challenge from server...");
1972
1970
  return [4 /*yield*/, axios.post(MFAEndpoints.RetrieveAuthChallenge, {}, {
1973
- headers: {
1974
- "X-DEVICE-ID": GenerateDeviceId(),
1975
- "X-API-VERSION": "1.0",
1976
- },
1971
+ headers: APIHeaders,
1977
1972
  })];
1978
1973
  case 1:
1979
- options = (_g.sent()).data;
1974
+ options = (_a.sent()).data;
1980
1975
  console.log("Received challenge:", options);
1981
1976
  // 2. Call the SDK to trigger the browser's passkey authentication UI
1982
1977
  console.log("Calling SDK authenticate function...");
1983
- return [4 /*yield*/, authenticate(__assign(__assign({}, options), { pubKeyCredParams: (_a = options.pubKeyCredParams) === null || _a === void 0 ? void 0 : _a.map(function (param) { return ({
1984
- type: "public-key",
1985
- alg: param.alg
1986
- }); }), attestation: ((_b = options.attestation) === null || _b === void 0 ? void 0 : _b.toLowerCase()) || "none", authenticatorSelection: __assign(__assign({}, options.authenticatorSelection), { residentKey: ((_d = (_c = options.authenticatorSelection) === null || _c === void 0 ? void 0 : _c.residentKey) === null || _d === void 0 ? void 0 : _d.toLowerCase()) || "discouraged", userVerification: ((_f = (_e = options.authenticatorSelection) === null || _e === void 0 ? void 0 : _e.userVerification) === null || _f === void 0 ? void 0 : _f.toLowerCase()) || "preferred" }) }))];
1978
+ return [4 /*yield*/, authenticate(__assign(__assign({}, options), { userVerification: "discouraged" }))];
1987
1979
  case 2:
1988
- assertionResponse = _g.sent();
1980
+ assertionResponse = _a.sent();
1989
1981
  console.log("Authentication assertion received from client:", assertionResponse);
1990
1982
  // 3. Send the assertion back to the server for verification
1991
1983
  console.log("Sending assertion to server for verification...");
1992
1984
  return [4 /*yield*/, axios.post(MFAEndpoints.AuthChallengeVerify, assertionResponse, {
1993
- headers: {
1994
- "X-DEVICE-ID": GenerateDeviceId(),
1995
- "X-API-VERSION": "1.0",
1996
- },
1985
+ headers: APIHeaders,
1997
1986
  withCredentials: true, // credentials: 'include'
1998
1987
  })];
1999
1988
  case 3:
2000
- _g.sent();
1989
+ _a.sent();
2001
1990
  sonner.toast.success("🔑 Sign-in successful!");
2002
1991
  return [3 /*break*/, 5];
2003
1992
  case 4:
2004
- error_1 = _g.sent();
1993
+ error_1 = _a.sent();
2005
1994
  console.error("Authentication failed:", error_1);
2006
1995
  sonner.toast.error("❌ Could not sign in.");
2007
1996
  return [3 /*break*/, 5];
@@ -2120,14 +2109,13 @@ var credentialsSchema = z.z.object({
2120
2109
  password: z.z.string().min(1, "Password is required"),
2121
2110
  });
2122
2111
  var ADLoginModal = function (_a) {
2123
- 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;
2124
2113
  var _b = React.useState("credentials"), step = _b[0], setStep = _b[1];
2125
2114
  var _c = React.useState({
2126
2115
  username: "",
2127
2116
  password: "",
2128
2117
  }), credentials = _c[0], setCredentials = _c[1];
2129
2118
  var _d = React.useState(""), mfaCode = _d[0], setMfaCode = _d[1];
2130
- var _e = React.useState(false), isLoading = _e[0], setIsLoading = _e[1];
2131
2119
  var form = reactHookForm.useForm({
2132
2120
  resolver: a$1(credentialsSchema),
2133
2121
  defaultValues: { username: "", password: "" },
@@ -2177,7 +2165,7 @@ var ADLoginModal = function (_a) {
2177
2165
  form.reset();
2178
2166
  setMfaCode("");
2179
2167
  };
2180
- 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) {
2181
2169
  var field = _a.field;
2182
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, {})] }));
2183
2171
  } }), jsxRuntimeExports.jsx(FormField, { control: form.control, name: "password", render: function (_a) {
@@ -2202,6 +2190,20 @@ var DefaultLoginPage = function (_a) {
2202
2190
  var login = context.login, isLoading = context.isLoading, authMode = context.authMode;
2203
2191
  var _b = React.useState(false), showADModal = _b[0], setShowADModal = _b[1];
2204
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;
2205
2207
  var handleMSALLogin = function () {
2206
2208
  if (authMode === "MSAL") {
2207
2209
  login();
@@ -2220,10 +2222,7 @@ var DefaultLoginPage = function (_a) {
2220
2222
  // 1. Fetch challenge from your server
2221
2223
  console.log("Requesting registration challenge from server...");
2222
2224
  return [4 /*yield*/, axios.post(MFAEndpoints.RegisterNewChallenge, __assign({}, data), {
2223
- headers: {
2224
- "X-DEVICE-ID": GenerateDeviceId(),
2225
- "X-API-VERSION": "1.0",
2226
- },
2225
+ headers: APIHeaders,
2227
2226
  })];
2228
2227
  case 1:
2229
2228
  options = (_a.sent()).data;
@@ -2237,10 +2236,7 @@ var DefaultLoginPage = function (_a) {
2237
2236
  // 3. Send the response back to the server for verification
2238
2237
  console.log("Sending attestation to server for verification...");
2239
2238
  return [4 /*yield*/, axios.post(MFAEndpoints.RegisterVerify, attestationResponse, {
2240
- headers: {
2241
- "X-DEVICE-ID": GenerateDeviceId(),
2242
- "X-API-VERSION": "1.0",
2243
- },
2239
+ headers: APIHeaders,
2244
2240
  withCredentials: true, // credentials: 'include'
2245
2241
  })];
2246
2242
  case 3:
@@ -2260,28 +2256,15 @@ var DefaultLoginPage = function (_a) {
2260
2256
  // variant="outline"
2261
2257
  , {
2262
2258
  // variant="outline"
2263
- 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
2264
2260
  ? "Logging in..."
2265
- : "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) {
2266
2262
  var username = _b.username, password = _b.password, MFACode = _b.MFACode;
2267
2263
  return __generator(this, function (_c) {
2268
2264
  // Implement your AD login logic here
2269
2265
  console.log("AD Login:", { username: username, password: password, MFACode: MFACode });
2270
2266
  // Example: await adLoginService(username, password, mfaCode);
2271
- 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 () {
2272
- return __generator(this, function (_a) {
2273
- console.log(data);
2274
- if (state && data) {
2275
- context.isAuthenticated = true;
2276
- context.requiresMFA = false;
2277
- context.setUserProfile({
2278
- type: "AUTH_SUCCESS",
2279
- userProfile: __assign({}, data),
2280
- });
2281
- }
2282
- return [2 /*return*/];
2283
- });
2284
- }); });
2267
+ handleSubmitCredentials(CredentialsAuthEndpoint !== null && CredentialsAuthEndpoint !== void 0 ? CredentialsAuthEndpoint : "/api/auth/validate", { username: username, password: password, MFACode: MFACode }, context.appCode);
2285
2268
  return [2 /*return*/];
2286
2269
  });
2287
2270
  }); } })] }));
@@ -2329,8 +2312,10 @@ var MFAGate = function (_a) {
2329
2312
  });
2330
2313
  }); }, [context.logout]);
2331
2314
  camsSdk.Logger.debug("MFA Endpoint >>>", { MFAEndpoints: MFAEndpoints });
2315
+ if (useADLogin && !CredentialsAuthEndpoint)
2316
+ return jsxRuntimeExports.jsx(ErrorFallback, { message: "Invalid AD Login Configuration." });
2332
2317
  if (!validatedMFAEndpoints)
2333
- return jsxRuntimeExports.jsx(ErrorFallback, { message: "Invalid MFA configuration." });
2318
+ return jsxRuntimeExports.jsx(ErrorFallback, { message: "Invalid MFA Configuration." });
2334
2319
  if (context.authMode !== "MSAL")
2335
2320
  return jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: children });
2336
2321
  if (context.isLoading)