@nibssplc/cams-sdk-react 0.0.1-beta.33 → 0.0.1-beta.35
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/components/CAMSMSALProvider.d.ts +0 -3
- package/dist/index.cjs.js +121 -68
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +122 -69
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React__default from 'react';
|
|
2
2
|
import React__default__default, { useState, useRef, useEffect, useCallback, useMemo, useReducer, useContext, createContext } from 'react';
|
|
3
|
-
import { CAMSSessionManager, CAMSError, CAMSErrorType } from '@nibssplc/cams-sdk';
|
|
3
|
+
import { CAMSSessionManager, CAMSError, CAMSErrorType, Logger as Logger$1 } from '@nibssplc/cams-sdk';
|
|
4
4
|
export * from '@nibssplc/cams-sdk';
|
|
5
5
|
|
|
6
6
|
/******************************************************************************
|
|
@@ -16702,63 +16702,118 @@ function useCAMSMSALAuth(options) {
|
|
|
16702
16702
|
var isLoading = inProgress !== InteractionStatus.None;
|
|
16703
16703
|
var isAuthenticated = !!account && !!token;
|
|
16704
16704
|
var scopes = options.scopes || ["openid", "profile", "email"];
|
|
16705
|
+
var isTokenValid = function (token) {
|
|
16706
|
+
try {
|
|
16707
|
+
var payload = JSON.parse(atob(token.split('.')[1]));
|
|
16708
|
+
return payload.exp * 1000 > Date.now();
|
|
16709
|
+
}
|
|
16710
|
+
catch (_a) {
|
|
16711
|
+
return false;
|
|
16712
|
+
}
|
|
16713
|
+
};
|
|
16714
|
+
useEffect(function () {
|
|
16715
|
+
if (typeof window !== 'undefined' && !token) {
|
|
16716
|
+
var stored = localStorage.getItem('cams-msal-auth');
|
|
16717
|
+
if (stored) {
|
|
16718
|
+
try {
|
|
16719
|
+
var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken;
|
|
16720
|
+
if (accessToken_1 && isTokenValid(accessToken_1)) {
|
|
16721
|
+
setToken(accessToken_1);
|
|
16722
|
+
setAccessToken(accessToken_1);
|
|
16723
|
+
setIdToken(idToken_1);
|
|
16724
|
+
}
|
|
16725
|
+
else {
|
|
16726
|
+
localStorage.removeItem('cams-msal-auth');
|
|
16727
|
+
}
|
|
16728
|
+
}
|
|
16729
|
+
catch (_b) { }
|
|
16730
|
+
}
|
|
16731
|
+
}
|
|
16732
|
+
}, [token]);
|
|
16733
|
+
// useEffect(() => {
|
|
16734
|
+
// const handleRedirect = async () => {
|
|
16735
|
+
// try {
|
|
16736
|
+
// const response = await instance.handleRedirectPromise();
|
|
16737
|
+
// if (response) {
|
|
16738
|
+
// const account = response.account;
|
|
16739
|
+
// instance.setActiveAccount(account);
|
|
16740
|
+
// const tokenResponse = await instance.acquireTokenSilent({
|
|
16741
|
+
// scopes,
|
|
16742
|
+
// account,
|
|
16743
|
+
// });
|
|
16744
|
+
// setToken(tokenResponse.accessToken);
|
|
16745
|
+
// setAccessToken(tokenResponse.accessToken);
|
|
16746
|
+
// setIdToken(tokenResponse.idToken);
|
|
16747
|
+
// options.onAuthSuccess?.(tokenResponse.accessToken);
|
|
16748
|
+
// if (
|
|
16749
|
+
// typeof window !== "undefined" &&
|
|
16750
|
+
// process.env.NODE_ENV !== "test"
|
|
16751
|
+
// ) {
|
|
16752
|
+
// window.location.href = options.mfaUrl!;
|
|
16753
|
+
// }
|
|
16754
|
+
// }
|
|
16755
|
+
// } catch (err) {
|
|
16756
|
+
// console.error("Redirect handling failed:", err);
|
|
16757
|
+
// }
|
|
16758
|
+
// };
|
|
16759
|
+
// handleRedirect();
|
|
16760
|
+
// }, []);
|
|
16705
16761
|
var login = useCallback(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
16706
|
-
var
|
|
16707
|
-
var _a, _b, _c
|
|
16708
|
-
return __generator(this, function (
|
|
16709
|
-
switch (
|
|
16762
|
+
var err_1, camsError_1, camsError;
|
|
16763
|
+
var _a, _b, _c;
|
|
16764
|
+
return __generator(this, function (_d) {
|
|
16765
|
+
switch (_d.label) {
|
|
16710
16766
|
case 0:
|
|
16711
16767
|
setError(null);
|
|
16712
|
-
|
|
16768
|
+
_d.label = 1;
|
|
16713
16769
|
case 1:
|
|
16714
|
-
|
|
16715
|
-
|
|
16770
|
+
_d.trys.push([1, 3, , 4]);
|
|
16771
|
+
// await instance.loginRedirect({
|
|
16772
|
+
// scopes,
|
|
16773
|
+
// prompt: options.prompt || "login",
|
|
16774
|
+
// });
|
|
16775
|
+
return [4 /*yield*/, instance
|
|
16776
|
+
.loginPopup({
|
|
16716
16777
|
scopes: scopes,
|
|
16717
16778
|
prompt: options.prompt || "login",
|
|
16779
|
+
})
|
|
16780
|
+
.then(function (response) {
|
|
16781
|
+
var _a;
|
|
16782
|
+
Logger$1.debug("Login Token response:", {
|
|
16783
|
+
accessToken: accessToken,
|
|
16784
|
+
idToken: idToken
|
|
16785
|
+
});
|
|
16786
|
+
setToken(response.accessToken);
|
|
16787
|
+
setAccessToken(response.accessToken);
|
|
16788
|
+
setIdToken(response.idToken);
|
|
16789
|
+
(_a = options.onAuthSuccess) === null || _a === void 0 ? void 0 : _a.call(options, response.accessToken);
|
|
16790
|
+
if (typeof window !== "undefined" &&
|
|
16791
|
+
process.env.NODE_ENV !== "test") {
|
|
16792
|
+
window.location.href = options.mfaUrl;
|
|
16793
|
+
}
|
|
16718
16794
|
})];
|
|
16719
16795
|
case 2:
|
|
16720
|
-
|
|
16721
|
-
|
|
16722
|
-
|
|
16723
|
-
|
|
16724
|
-
|
|
16725
|
-
|
|
16796
|
+
// await instance.loginRedirect({
|
|
16797
|
+
// scopes,
|
|
16798
|
+
// prompt: options.prompt || "login",
|
|
16799
|
+
// });
|
|
16800
|
+
_d.sent();
|
|
16801
|
+
return [3 /*break*/, 4];
|
|
16726
16802
|
case 3:
|
|
16727
|
-
|
|
16728
|
-
|
|
16729
|
-
|
|
16730
|
-
|
|
16731
|
-
|
|
16732
|
-
|
|
16803
|
+
err_1 = _d.sent();
|
|
16804
|
+
// If popup is blocked
|
|
16805
|
+
if (err_1.errorCode === "popup_window_error" ||
|
|
16806
|
+
((_a = err_1.message) === null || _a === void 0 ? void 0 : _a.includes("popup"))) {
|
|
16807
|
+
camsError_1 = new CAMSError(CAMSErrorType.POPUP_BLOCKED, "Both popup and redirect failed: " + err_1);
|
|
16808
|
+
setError(camsError_1);
|
|
16809
|
+
(_b = options.onAuthError) === null || _b === void 0 ? void 0 : _b.call(options, camsError_1);
|
|
16810
|
+
return [2 /*return*/];
|
|
16733
16811
|
}
|
|
16734
|
-
(_a = options.onAuthSuccess) === null || _a === void 0 ? void 0 : _a.call(options, tokenResponse.accessToken);
|
|
16735
|
-
return [3 /*break*/, 9];
|
|
16736
|
-
case 4:
|
|
16737
|
-
err_1 = _e.sent();
|
|
16738
|
-
if (!(err_1.errorCode === "popup_window_error" ||
|
|
16739
|
-
((_b = err_1.message) === null || _b === void 0 ? void 0 : _b.includes("popup")))) return [3 /*break*/, 8];
|
|
16740
|
-
_e.label = 5;
|
|
16741
|
-
case 5:
|
|
16742
|
-
_e.trys.push([5, 7, , 8]);
|
|
16743
|
-
return [4 /*yield*/, instance.loginRedirect({
|
|
16744
|
-
scopes: scopes,
|
|
16745
|
-
prompt: options.prompt || "login",
|
|
16746
|
-
})];
|
|
16747
|
-
case 6:
|
|
16748
|
-
_e.sent();
|
|
16749
|
-
return [2 /*return*/];
|
|
16750
|
-
case 7:
|
|
16751
|
-
redirectErr_1 = _e.sent();
|
|
16752
|
-
camsError_1 = new CAMSError(CAMSErrorType.POPUP_BLOCKED, "Both popup and redirect failed: " + redirectErr_1);
|
|
16753
|
-
setError(camsError_1);
|
|
16754
|
-
(_c = options.onAuthError) === null || _c === void 0 ? void 0 : _c.call(options, camsError_1);
|
|
16755
|
-
return [2 /*return*/];
|
|
16756
|
-
case 8:
|
|
16757
16812
|
camsError = new CAMSError(CAMSErrorType.API_VALIDATION_ERROR, "Login failed: " + err_1);
|
|
16758
16813
|
setError(camsError);
|
|
16759
|
-
(
|
|
16760
|
-
return [3 /*break*/,
|
|
16761
|
-
case
|
|
16814
|
+
(_c = options.onAuthError) === null || _c === void 0 ? void 0 : _c.call(options, camsError);
|
|
16815
|
+
return [3 /*break*/, 4];
|
|
16816
|
+
case 4: return [2 /*return*/];
|
|
16762
16817
|
}
|
|
16763
16818
|
});
|
|
16764
16819
|
}); }, [instance, scopes, options]);
|
|
@@ -16772,7 +16827,12 @@ function useCAMSMSALAuth(options) {
|
|
|
16772
16827
|
case 1:
|
|
16773
16828
|
_a.sent();
|
|
16774
16829
|
setToken(null);
|
|
16830
|
+
setAccessToken(null);
|
|
16831
|
+
setIdToken(null);
|
|
16775
16832
|
setError(null);
|
|
16833
|
+
if (typeof window !== 'undefined') {
|
|
16834
|
+
localStorage.removeItem('cams-msal-auth');
|
|
16835
|
+
}
|
|
16776
16836
|
return [3 /*break*/, 3];
|
|
16777
16837
|
case 2:
|
|
16778
16838
|
err_2 = _a.sent();
|
|
@@ -17247,34 +17307,27 @@ function ProtectedRoute(_a) {
|
|
|
17247
17307
|
}
|
|
17248
17308
|
|
|
17249
17309
|
var CAMSMSALContext = createContext(null);
|
|
17310
|
+
var isTokenValid = function (token) {
|
|
17311
|
+
try {
|
|
17312
|
+
var payload = JSON.parse(atob(token.split('.')[1]));
|
|
17313
|
+
return payload.exp * 1000 > Date.now();
|
|
17314
|
+
}
|
|
17315
|
+
catch (_a) {
|
|
17316
|
+
return false;
|
|
17317
|
+
}
|
|
17318
|
+
};
|
|
17250
17319
|
function CAMSMSALProviderInner(_a) {
|
|
17251
17320
|
var children = _a.children, authOptions = __rest(_a, ["children"]);
|
|
17252
17321
|
var auth = useCAMSMSALAuth(authOptions);
|
|
17253
|
-
var _b = useState({
|
|
17254
|
-
isAuthenticated: false,
|
|
17255
|
-
idToken: null,
|
|
17256
|
-
accessToken: null
|
|
17257
|
-
}), persistedAuth = _b[0], setPersistedAuth = _b[1];
|
|
17258
|
-
useEffect(function () {
|
|
17259
|
-
if (typeof window !== 'undefined') {
|
|
17260
|
-
var stored = localStorage.getItem('cams-msal-auth');
|
|
17261
|
-
if (stored) {
|
|
17262
|
-
setPersistedAuth(JSON.parse(stored));
|
|
17263
|
-
}
|
|
17264
|
-
}
|
|
17265
|
-
}, []);
|
|
17266
17322
|
useEffect(function () {
|
|
17267
|
-
|
|
17268
|
-
|
|
17269
|
-
|
|
17270
|
-
|
|
17271
|
-
|
|
17272
|
-
setPersistedAuth(authState);
|
|
17273
|
-
if (typeof window !== 'undefined') {
|
|
17274
|
-
localStorage.setItem('cams-msal-auth', JSON.stringify(authState));
|
|
17323
|
+
if (auth.accessToken && isTokenValid(auth.accessToken) && typeof window !== 'undefined') {
|
|
17324
|
+
localStorage.setItem('cams-msal-auth', JSON.stringify({
|
|
17325
|
+
accessToken: auth.accessToken,
|
|
17326
|
+
idToken: auth.idToken
|
|
17327
|
+
}));
|
|
17275
17328
|
}
|
|
17276
|
-
}, [auth.
|
|
17277
|
-
var value = useMemo(function () { return
|
|
17329
|
+
}, [auth.accessToken, auth.idToken]);
|
|
17330
|
+
var value = useMemo(function () { return auth; }, [auth]);
|
|
17278
17331
|
return (jsxRuntimeExports.jsx(CAMSMSALContext.Provider, { value: value, children: children }));
|
|
17279
17332
|
}
|
|
17280
17333
|
function CAMSMSALProvider(props) {
|