@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.
@@ -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,9 +16798,21 @@ 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);
16808
+ // Persist tokens to localStorage
16809
+ if (typeof window !== 'undefined') {
16810
+ localStorage.setItem('cams-msal-auth', JSON.stringify({
16811
+ isAuthenticated: true,
16812
+ accessToken: response.accessToken,
16813
+ idToken: response.idToken
16814
+ }));
16815
+ }
16776
16816
  (_a = options.onAuthSuccess) === null || _a === void 0 ? void 0 : _a.call(options, response.accessToken);
16777
16817
  if (typeof window !== "undefined" &&
16778
16818
  process.env.NODE_ENV !== "test") {
@@ -16814,7 +16854,12 @@ function useCAMSMSALAuth(options) {
16814
16854
  case 1:
16815
16855
  _a.sent();
16816
16856
  setToken(null);
16857
+ setAccessToken(null);
16858
+ setIdToken(null);
16817
16859
  setError(null);
16860
+ if (typeof window !== 'undefined') {
16861
+ localStorage.removeItem('cams-msal-auth');
16862
+ }
16818
16863
  return [3 /*break*/, 3];
16819
16864
  case 2:
16820
16865
  err_2 = _a.sent();
@@ -17289,34 +17334,27 @@ function ProtectedRoute(_a) {
17289
17334
  }
17290
17335
 
17291
17336
  var CAMSMSALContext = React__default.createContext(null);
17337
+ var isTokenValid = function (token) {
17338
+ try {
17339
+ var payload = JSON.parse(atob(token.split('.')[1]));
17340
+ return payload.exp * 1000 > Date.now();
17341
+ }
17342
+ catch (_a) {
17343
+ return false;
17344
+ }
17345
+ };
17292
17346
  function CAMSMSALProviderInner(_a) {
17293
17347
  var children = _a.children, authOptions = __rest(_a, ["children"]);
17294
17348
  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
17349
  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));
17350
+ if (auth.accessToken && isTokenValid(auth.accessToken) && typeof window !== 'undefined') {
17351
+ localStorage.setItem('cams-msal-auth', JSON.stringify({
17352
+ accessToken: auth.accessToken,
17353
+ idToken: auth.idToken
17354
+ }));
17317
17355
  }
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]);
17356
+ }, [auth.accessToken, auth.idToken]);
17357
+ var value = React__default.useMemo(function () { return auth; }, [auth]);
17320
17358
  return (jsxRuntimeExports.jsx(CAMSMSALContext.Provider, { value: value, children: children }));
17321
17359
  }
17322
17360
  function CAMSMSALProvider(props) {