@nibssplc/cams-sdk-react 0.0.1-beta.34 → 0.0.1-beta.36
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 +61 -23
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +62 -24
- 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,6 +16702,34 @@ 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]);
|
|
16705
16733
|
// useEffect(() => {
|
|
16706
16734
|
// const handleRedirect = async () => {
|
|
16707
16735
|
// try {
|
|
@@ -16751,9 +16779,21 @@ function useCAMSMSALAuth(options) {
|
|
|
16751
16779
|
})
|
|
16752
16780
|
.then(function (response) {
|
|
16753
16781
|
var _a;
|
|
16782
|
+
Logger$1.debug("Login Token response:", {
|
|
16783
|
+
accessToken: accessToken,
|
|
16784
|
+
idToken: idToken
|
|
16785
|
+
});
|
|
16754
16786
|
setToken(response.accessToken);
|
|
16755
16787
|
setAccessToken(response.accessToken);
|
|
16756
16788
|
setIdToken(response.idToken);
|
|
16789
|
+
// Persist tokens to localStorage
|
|
16790
|
+
if (typeof window !== 'undefined') {
|
|
16791
|
+
localStorage.setItem('cams-msal-auth', JSON.stringify({
|
|
16792
|
+
isAuthenticated: true,
|
|
16793
|
+
accessToken: response.accessToken,
|
|
16794
|
+
idToken: response.idToken
|
|
16795
|
+
}));
|
|
16796
|
+
}
|
|
16757
16797
|
(_a = options.onAuthSuccess) === null || _a === void 0 ? void 0 : _a.call(options, response.accessToken);
|
|
16758
16798
|
if (typeof window !== "undefined" &&
|
|
16759
16799
|
process.env.NODE_ENV !== "test") {
|
|
@@ -16795,7 +16835,12 @@ function useCAMSMSALAuth(options) {
|
|
|
16795
16835
|
case 1:
|
|
16796
16836
|
_a.sent();
|
|
16797
16837
|
setToken(null);
|
|
16838
|
+
setAccessToken(null);
|
|
16839
|
+
setIdToken(null);
|
|
16798
16840
|
setError(null);
|
|
16841
|
+
if (typeof window !== 'undefined') {
|
|
16842
|
+
localStorage.removeItem('cams-msal-auth');
|
|
16843
|
+
}
|
|
16799
16844
|
return [3 /*break*/, 3];
|
|
16800
16845
|
case 2:
|
|
16801
16846
|
err_2 = _a.sent();
|
|
@@ -17270,34 +17315,27 @@ function ProtectedRoute(_a) {
|
|
|
17270
17315
|
}
|
|
17271
17316
|
|
|
17272
17317
|
var CAMSMSALContext = createContext(null);
|
|
17318
|
+
var isTokenValid = function (token) {
|
|
17319
|
+
try {
|
|
17320
|
+
var payload = JSON.parse(atob(token.split('.')[1]));
|
|
17321
|
+
return payload.exp * 1000 > Date.now();
|
|
17322
|
+
}
|
|
17323
|
+
catch (_a) {
|
|
17324
|
+
return false;
|
|
17325
|
+
}
|
|
17326
|
+
};
|
|
17273
17327
|
function CAMSMSALProviderInner(_a) {
|
|
17274
17328
|
var children = _a.children, authOptions = __rest(_a, ["children"]);
|
|
17275
17329
|
var auth = useCAMSMSALAuth(authOptions);
|
|
17276
|
-
var _b = useState({
|
|
17277
|
-
isAuthenticated: false,
|
|
17278
|
-
idToken: null,
|
|
17279
|
-
accessToken: null
|
|
17280
|
-
}), persistedAuth = _b[0], setPersistedAuth = _b[1];
|
|
17281
|
-
useEffect(function () {
|
|
17282
|
-
if (typeof window !== 'undefined') {
|
|
17283
|
-
var stored = localStorage.getItem('cams-msal-auth');
|
|
17284
|
-
if (stored) {
|
|
17285
|
-
setPersistedAuth(JSON.parse(stored));
|
|
17286
|
-
}
|
|
17287
|
-
}
|
|
17288
|
-
}, []);
|
|
17289
17330
|
useEffect(function () {
|
|
17290
|
-
|
|
17291
|
-
|
|
17292
|
-
|
|
17293
|
-
|
|
17294
|
-
|
|
17295
|
-
setPersistedAuth(authState);
|
|
17296
|
-
if (typeof window !== 'undefined') {
|
|
17297
|
-
localStorage.setItem('cams-msal-auth', JSON.stringify(authState));
|
|
17331
|
+
if (auth.accessToken && isTokenValid(auth.accessToken) && typeof window !== 'undefined') {
|
|
17332
|
+
localStorage.setItem('cams-msal-auth', JSON.stringify({
|
|
17333
|
+
accessToken: auth.accessToken,
|
|
17334
|
+
idToken: auth.idToken
|
|
17335
|
+
}));
|
|
17298
17336
|
}
|
|
17299
|
-
}, [auth.
|
|
17300
|
-
var value = useMemo(function () { return
|
|
17337
|
+
}, [auth.accessToken, auth.idToken]);
|
|
17338
|
+
var value = useMemo(function () { return auth; }, [auth]);
|
|
17301
17339
|
return (jsxRuntimeExports.jsx(CAMSMSALContext.Provider, { value: value, children: children }));
|
|
17302
17340
|
}
|
|
17303
17341
|
function CAMSMSALProvider(props) {
|