@nibssplc/cams-sdk-react 0.0.1-beta.34 → 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/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,6 +16779,10 @@ 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);
@@ -16795,7 +16827,12 @@ function useCAMSMSALAuth(options) {
16795
16827
  case 1:
16796
16828
  _a.sent();
16797
16829
  setToken(null);
16830
+ setAccessToken(null);
16831
+ setIdToken(null);
16798
16832
  setError(null);
16833
+ if (typeof window !== 'undefined') {
16834
+ localStorage.removeItem('cams-msal-auth');
16835
+ }
16799
16836
  return [3 /*break*/, 3];
16800
16837
  case 2:
16801
16838
  err_2 = _a.sent();
@@ -17270,34 +17307,27 @@ function ProtectedRoute(_a) {
17270
17307
  }
17271
17308
 
17272
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
+ };
17273
17319
  function CAMSMSALProviderInner(_a) {
17274
17320
  var children = _a.children, authOptions = __rest(_a, ["children"]);
17275
17321
  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
17322
  useEffect(function () {
17290
- var authState = {
17291
- isAuthenticated: auth.isAuthenticated,
17292
- idToken: auth.idToken,
17293
- accessToken: auth.accessToken
17294
- };
17295
- setPersistedAuth(authState);
17296
- if (typeof window !== 'undefined') {
17297
- 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
+ }));
17298
17328
  }
17299
- }, [auth.isAuthenticated, auth.idToken, auth.accessToken]);
17300
- var value = useMemo(function () { return (__assign(__assign({}, auth), { isAuthenticated: persistedAuth.isAuthenticated, idToken: persistedAuth.idToken, accessToken: persistedAuth.accessToken })); }, [auth, persistedAuth]);
17329
+ }, [auth.accessToken, auth.idToken]);
17330
+ var value = useMemo(function () { return auth; }, [auth]);
17301
17331
  return (jsxRuntimeExports.jsx(CAMSMSALContext.Provider, { value: value, children: children }));
17302
17332
  }
17303
17333
  function CAMSMSALProvider(props) {