@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.
@@ -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,6 +16721,34 @@ 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]);
16724
16752
  // useEffect(() => {
16725
16753
  // const handleRedirect = async () => {
16726
16754
  // try {
@@ -16770,6 +16798,10 @@ function useCAMSMSALAuth(options) {
16770
16798
  })
16771
16799
  .then(function (response) {
16772
16800
  var _a;
16801
+ camsSdk.Logger.debug("Login Token response:", {
16802
+ accessToken: accessToken,
16803
+ idToken: idToken
16804
+ });
16773
16805
  setToken(response.accessToken);
16774
16806
  setAccessToken(response.accessToken);
16775
16807
  setIdToken(response.idToken);
@@ -16814,7 +16846,12 @@ function useCAMSMSALAuth(options) {
16814
16846
  case 1:
16815
16847
  _a.sent();
16816
16848
  setToken(null);
16849
+ setAccessToken(null);
16850
+ setIdToken(null);
16817
16851
  setError(null);
16852
+ if (typeof window !== 'undefined') {
16853
+ localStorage.removeItem('cams-msal-auth');
16854
+ }
16818
16855
  return [3 /*break*/, 3];
16819
16856
  case 2:
16820
16857
  err_2 = _a.sent();
@@ -17289,34 +17326,27 @@ function ProtectedRoute(_a) {
17289
17326
  }
17290
17327
 
17291
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
+ };
17292
17338
  function CAMSMSALProviderInner(_a) {
17293
17339
  var children = _a.children, authOptions = __rest(_a, ["children"]);
17294
17340
  var auth = useCAMSMSALAuth(authOptions);
17295
- var _b = React__default.useState({
17296
- isAuthenticated: false,
17297
- idToken: null,
17298
- accessToken: null
17299
- }), persistedAuth = _b[0], setPersistedAuth = _b[1];
17300
- React__default.useEffect(function () {
17301
- if (typeof window !== 'undefined') {
17302
- var stored = localStorage.getItem('cams-msal-auth');
17303
- if (stored) {
17304
- setPersistedAuth(JSON.parse(stored));
17305
- }
17306
- }
17307
- }, []);
17308
17341
  React__default.useEffect(function () {
17309
- var authState = {
17310
- isAuthenticated: auth.isAuthenticated,
17311
- idToken: auth.idToken,
17312
- accessToken: auth.accessToken
17313
- };
17314
- setPersistedAuth(authState);
17315
- if (typeof window !== 'undefined') {
17316
- 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
+ }));
17317
17347
  }
17318
- }, [auth.isAuthenticated, auth.idToken, auth.accessToken]);
17319
- var value = React__default.useMemo(function () { return (__assign(__assign({}, auth), { isAuthenticated: persistedAuth.isAuthenticated, idToken: persistedAuth.idToken, accessToken: persistedAuth.accessToken })); }, [auth, persistedAuth]);
17348
+ }, [auth.accessToken, auth.idToken]);
17349
+ var value = React__default.useMemo(function () { return auth; }, [auth]);
17320
17350
  return (jsxRuntimeExports.jsx(CAMSMSALContext.Provider, { value: value, children: children }));
17321
17351
  }
17322
17352
  function CAMSMSALProvider(props) {