@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
|
@@ -2,9 +2,6 @@ import React from 'react';
|
|
|
2
2
|
import { PublicClientApplication, Configuration } from '@azure/msal-browser';
|
|
3
3
|
import { UseCAMSMSALAuthReturn, UseCAMSMSALAuthOptions } from '../hooks/useCAMSMSALAuth';
|
|
4
4
|
interface CAMSMSALContextValue extends UseCAMSMSALAuthReturn {
|
|
5
|
-
isAuthenticated: boolean;
|
|
6
|
-
idToken: string | null;
|
|
7
|
-
accessToken: string | null;
|
|
8
5
|
}
|
|
9
6
|
export interface CAMSMSALProviderProps extends UseCAMSMSALAuthOptions {
|
|
10
7
|
children: React.ReactNode;
|
package/dist/index.cjs.js
CHANGED
|
@@ -16721,63 +16721,118 @@ function useCAMSMSALAuth(options) {
|
|
|
16721
16721
|
var isLoading = inProgress !== InteractionStatus.None;
|
|
16722
16722
|
var isAuthenticated = !!account && !!token;
|
|
16723
16723
|
var scopes = options.scopes || ["openid", "profile", "email"];
|
|
16724
|
+
var isTokenValid = function (token) {
|
|
16725
|
+
try {
|
|
16726
|
+
var payload = JSON.parse(atob(token.split('.')[1]));
|
|
16727
|
+
return payload.exp * 1000 > Date.now();
|
|
16728
|
+
}
|
|
16729
|
+
catch (_a) {
|
|
16730
|
+
return false;
|
|
16731
|
+
}
|
|
16732
|
+
};
|
|
16733
|
+
React__default.useEffect(function () {
|
|
16734
|
+
if (typeof window !== 'undefined' && !token) {
|
|
16735
|
+
var stored = localStorage.getItem('cams-msal-auth');
|
|
16736
|
+
if (stored) {
|
|
16737
|
+
try {
|
|
16738
|
+
var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken;
|
|
16739
|
+
if (accessToken_1 && isTokenValid(accessToken_1)) {
|
|
16740
|
+
setToken(accessToken_1);
|
|
16741
|
+
setAccessToken(accessToken_1);
|
|
16742
|
+
setIdToken(idToken_1);
|
|
16743
|
+
}
|
|
16744
|
+
else {
|
|
16745
|
+
localStorage.removeItem('cams-msal-auth');
|
|
16746
|
+
}
|
|
16747
|
+
}
|
|
16748
|
+
catch (_b) { }
|
|
16749
|
+
}
|
|
16750
|
+
}
|
|
16751
|
+
}, [token]);
|
|
16752
|
+
// useEffect(() => {
|
|
16753
|
+
// const handleRedirect = async () => {
|
|
16754
|
+
// try {
|
|
16755
|
+
// const response = await instance.handleRedirectPromise();
|
|
16756
|
+
// if (response) {
|
|
16757
|
+
// const account = response.account;
|
|
16758
|
+
// instance.setActiveAccount(account);
|
|
16759
|
+
// const tokenResponse = await instance.acquireTokenSilent({
|
|
16760
|
+
// scopes,
|
|
16761
|
+
// account,
|
|
16762
|
+
// });
|
|
16763
|
+
// setToken(tokenResponse.accessToken);
|
|
16764
|
+
// setAccessToken(tokenResponse.accessToken);
|
|
16765
|
+
// setIdToken(tokenResponse.idToken);
|
|
16766
|
+
// options.onAuthSuccess?.(tokenResponse.accessToken);
|
|
16767
|
+
// if (
|
|
16768
|
+
// typeof window !== "undefined" &&
|
|
16769
|
+
// process.env.NODE_ENV !== "test"
|
|
16770
|
+
// ) {
|
|
16771
|
+
// window.location.href = options.mfaUrl!;
|
|
16772
|
+
// }
|
|
16773
|
+
// }
|
|
16774
|
+
// } catch (err) {
|
|
16775
|
+
// console.error("Redirect handling failed:", err);
|
|
16776
|
+
// }
|
|
16777
|
+
// };
|
|
16778
|
+
// handleRedirect();
|
|
16779
|
+
// }, []);
|
|
16724
16780
|
var login = React__default.useCallback(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
16725
|
-
var
|
|
16726
|
-
var _a, _b, _c
|
|
16727
|
-
return __generator(this, function (
|
|
16728
|
-
switch (
|
|
16781
|
+
var err_1, camsError_1, camsError;
|
|
16782
|
+
var _a, _b, _c;
|
|
16783
|
+
return __generator(this, function (_d) {
|
|
16784
|
+
switch (_d.label) {
|
|
16729
16785
|
case 0:
|
|
16730
16786
|
setError(null);
|
|
16731
|
-
|
|
16787
|
+
_d.label = 1;
|
|
16732
16788
|
case 1:
|
|
16733
|
-
|
|
16734
|
-
|
|
16789
|
+
_d.trys.push([1, 3, , 4]);
|
|
16790
|
+
// await instance.loginRedirect({
|
|
16791
|
+
// scopes,
|
|
16792
|
+
// prompt: options.prompt || "login",
|
|
16793
|
+
// });
|
|
16794
|
+
return [4 /*yield*/, instance
|
|
16795
|
+
.loginPopup({
|
|
16735
16796
|
scopes: scopes,
|
|
16736
16797
|
prompt: options.prompt || "login",
|
|
16798
|
+
})
|
|
16799
|
+
.then(function (response) {
|
|
16800
|
+
var _a;
|
|
16801
|
+
camsSdk.Logger.debug("Login Token response:", {
|
|
16802
|
+
accessToken: accessToken,
|
|
16803
|
+
idToken: idToken
|
|
16804
|
+
});
|
|
16805
|
+
setToken(response.accessToken);
|
|
16806
|
+
setAccessToken(response.accessToken);
|
|
16807
|
+
setIdToken(response.idToken);
|
|
16808
|
+
(_a = options.onAuthSuccess) === null || _a === void 0 ? void 0 : _a.call(options, response.accessToken);
|
|
16809
|
+
if (typeof window !== "undefined" &&
|
|
16810
|
+
process.env.NODE_ENV !== "test") {
|
|
16811
|
+
window.location.href = options.mfaUrl;
|
|
16812
|
+
}
|
|
16737
16813
|
})];
|
|
16738
16814
|
case 2:
|
|
16739
|
-
|
|
16740
|
-
|
|
16741
|
-
|
|
16742
|
-
|
|
16743
|
-
|
|
16744
|
-
|
|
16815
|
+
// await instance.loginRedirect({
|
|
16816
|
+
// scopes,
|
|
16817
|
+
// prompt: options.prompt || "login",
|
|
16818
|
+
// });
|
|
16819
|
+
_d.sent();
|
|
16820
|
+
return [3 /*break*/, 4];
|
|
16745
16821
|
case 3:
|
|
16746
|
-
|
|
16747
|
-
|
|
16748
|
-
|
|
16749
|
-
|
|
16750
|
-
|
|
16751
|
-
|
|
16822
|
+
err_1 = _d.sent();
|
|
16823
|
+
// If popup is blocked
|
|
16824
|
+
if (err_1.errorCode === "popup_window_error" ||
|
|
16825
|
+
((_a = err_1.message) === null || _a === void 0 ? void 0 : _a.includes("popup"))) {
|
|
16826
|
+
camsError_1 = new camsSdk.CAMSError(camsSdk.CAMSErrorType.POPUP_BLOCKED, "Both popup and redirect failed: " + err_1);
|
|
16827
|
+
setError(camsError_1);
|
|
16828
|
+
(_b = options.onAuthError) === null || _b === void 0 ? void 0 : _b.call(options, camsError_1);
|
|
16829
|
+
return [2 /*return*/];
|
|
16752
16830
|
}
|
|
16753
|
-
(_a = options.onAuthSuccess) === null || _a === void 0 ? void 0 : _a.call(options, tokenResponse.accessToken);
|
|
16754
|
-
return [3 /*break*/, 9];
|
|
16755
|
-
case 4:
|
|
16756
|
-
err_1 = _e.sent();
|
|
16757
|
-
if (!(err_1.errorCode === "popup_window_error" ||
|
|
16758
|
-
((_b = err_1.message) === null || _b === void 0 ? void 0 : _b.includes("popup")))) return [3 /*break*/, 8];
|
|
16759
|
-
_e.label = 5;
|
|
16760
|
-
case 5:
|
|
16761
|
-
_e.trys.push([5, 7, , 8]);
|
|
16762
|
-
return [4 /*yield*/, instance.loginRedirect({
|
|
16763
|
-
scopes: scopes,
|
|
16764
|
-
prompt: options.prompt || "login",
|
|
16765
|
-
})];
|
|
16766
|
-
case 6:
|
|
16767
|
-
_e.sent();
|
|
16768
|
-
return [2 /*return*/];
|
|
16769
|
-
case 7:
|
|
16770
|
-
redirectErr_1 = _e.sent();
|
|
16771
|
-
camsError_1 = new camsSdk.CAMSError(camsSdk.CAMSErrorType.POPUP_BLOCKED, "Both popup and redirect failed: " + redirectErr_1);
|
|
16772
|
-
setError(camsError_1);
|
|
16773
|
-
(_c = options.onAuthError) === null || _c === void 0 ? void 0 : _c.call(options, camsError_1);
|
|
16774
|
-
return [2 /*return*/];
|
|
16775
|
-
case 8:
|
|
16776
16831
|
camsError = new camsSdk.CAMSError(camsSdk.CAMSErrorType.API_VALIDATION_ERROR, "Login failed: " + err_1);
|
|
16777
16832
|
setError(camsError);
|
|
16778
|
-
(
|
|
16779
|
-
return [3 /*break*/,
|
|
16780
|
-
case
|
|
16833
|
+
(_c = options.onAuthError) === null || _c === void 0 ? void 0 : _c.call(options, camsError);
|
|
16834
|
+
return [3 /*break*/, 4];
|
|
16835
|
+
case 4: return [2 /*return*/];
|
|
16781
16836
|
}
|
|
16782
16837
|
});
|
|
16783
16838
|
}); }, [instance, scopes, options]);
|
|
@@ -16791,7 +16846,12 @@ function useCAMSMSALAuth(options) {
|
|
|
16791
16846
|
case 1:
|
|
16792
16847
|
_a.sent();
|
|
16793
16848
|
setToken(null);
|
|
16849
|
+
setAccessToken(null);
|
|
16850
|
+
setIdToken(null);
|
|
16794
16851
|
setError(null);
|
|
16852
|
+
if (typeof window !== 'undefined') {
|
|
16853
|
+
localStorage.removeItem('cams-msal-auth');
|
|
16854
|
+
}
|
|
16795
16855
|
return [3 /*break*/, 3];
|
|
16796
16856
|
case 2:
|
|
16797
16857
|
err_2 = _a.sent();
|
|
@@ -17266,34 +17326,27 @@ function ProtectedRoute(_a) {
|
|
|
17266
17326
|
}
|
|
17267
17327
|
|
|
17268
17328
|
var CAMSMSALContext = React__default.createContext(null);
|
|
17329
|
+
var isTokenValid = function (token) {
|
|
17330
|
+
try {
|
|
17331
|
+
var payload = JSON.parse(atob(token.split('.')[1]));
|
|
17332
|
+
return payload.exp * 1000 > Date.now();
|
|
17333
|
+
}
|
|
17334
|
+
catch (_a) {
|
|
17335
|
+
return false;
|
|
17336
|
+
}
|
|
17337
|
+
};
|
|
17269
17338
|
function CAMSMSALProviderInner(_a) {
|
|
17270
17339
|
var children = _a.children, authOptions = __rest(_a, ["children"]);
|
|
17271
17340
|
var auth = useCAMSMSALAuth(authOptions);
|
|
17272
|
-
var _b = React__default.useState({
|
|
17273
|
-
isAuthenticated: false,
|
|
17274
|
-
idToken: null,
|
|
17275
|
-
accessToken: null
|
|
17276
|
-
}), persistedAuth = _b[0], setPersistedAuth = _b[1];
|
|
17277
|
-
React__default.useEffect(function () {
|
|
17278
|
-
if (typeof window !== 'undefined') {
|
|
17279
|
-
var stored = localStorage.getItem('cams-msal-auth');
|
|
17280
|
-
if (stored) {
|
|
17281
|
-
setPersistedAuth(JSON.parse(stored));
|
|
17282
|
-
}
|
|
17283
|
-
}
|
|
17284
|
-
}, []);
|
|
17285
17341
|
React__default.useEffect(function () {
|
|
17286
|
-
|
|
17287
|
-
|
|
17288
|
-
|
|
17289
|
-
|
|
17290
|
-
|
|
17291
|
-
setPersistedAuth(authState);
|
|
17292
|
-
if (typeof window !== 'undefined') {
|
|
17293
|
-
localStorage.setItem('cams-msal-auth', JSON.stringify(authState));
|
|
17342
|
+
if (auth.accessToken && isTokenValid(auth.accessToken) && typeof window !== 'undefined') {
|
|
17343
|
+
localStorage.setItem('cams-msal-auth', JSON.stringify({
|
|
17344
|
+
accessToken: auth.accessToken,
|
|
17345
|
+
idToken: auth.idToken
|
|
17346
|
+
}));
|
|
17294
17347
|
}
|
|
17295
|
-
}, [auth.
|
|
17296
|
-
var value = React__default.useMemo(function () { return
|
|
17348
|
+
}, [auth.accessToken, auth.idToken]);
|
|
17349
|
+
var value = React__default.useMemo(function () { return auth; }, [auth]);
|
|
17297
17350
|
return (jsxRuntimeExports.jsx(CAMSMSALContext.Provider, { value: value, children: children }));
|
|
17298
17351
|
}
|
|
17299
17352
|
function CAMSMSALProvider(props) {
|