@nibssplc/cams-sdk-react 0.0.1-beta.36 → 0.0.1-beta.37

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
@@ -16695,6 +16695,7 @@ function useCAMSMSALAuth(options) {
16695
16695
  }; }
16696
16696
  var _a = useMsal(), instance = _a.instance, inProgress = _a.inProgress, accounts = _a.accounts;
16697
16697
  var account = useAccount(accounts[0] || {});
16698
+ var storageKey = 'CAMS-MSAL-AUTH-SDK';
16698
16699
  var _b = useState(null), error = _b[0], setError = _b[1];
16699
16700
  var _c = useState(null), token = _c[0], setToken = _c[1];
16700
16701
  var _d = useState(null), idToken = _d[0], setIdToken = _d[1];
@@ -16713,7 +16714,7 @@ function useCAMSMSALAuth(options) {
16713
16714
  };
16714
16715
  useEffect(function () {
16715
16716
  if (typeof window !== 'undefined' && !token) {
16716
- var stored = localStorage.getItem('cams-msal-auth');
16717
+ var stored = localStorage.getItem(storageKey);
16717
16718
  if (stored) {
16718
16719
  try {
16719
16720
  var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken;
@@ -16723,7 +16724,7 @@ function useCAMSMSALAuth(options) {
16723
16724
  setIdToken(idToken_1);
16724
16725
  }
16725
16726
  else {
16726
- localStorage.removeItem('cams-msal-auth');
16727
+ localStorage.removeItem(storageKey);
16727
16728
  }
16728
16729
  }
16729
16730
  catch (_b) { }
@@ -16788,7 +16789,7 @@ function useCAMSMSALAuth(options) {
16788
16789
  setIdToken(response.idToken);
16789
16790
  // Persist tokens to localStorage
16790
16791
  if (typeof window !== 'undefined') {
16791
- localStorage.setItem('cams-msal-auth', JSON.stringify({
16792
+ localStorage.setItem(storageKey, JSON.stringify({
16792
16793
  isAuthenticated: true,
16793
16794
  accessToken: response.accessToken,
16794
16795
  idToken: response.idToken
@@ -16839,7 +16840,7 @@ function useCAMSMSALAuth(options) {
16839
16840
  setIdToken(null);
16840
16841
  setError(null);
16841
16842
  if (typeof window !== 'undefined') {
16842
- localStorage.removeItem('cams-msal-auth');
16843
+ localStorage.removeItem(storageKey);
16843
16844
  }
16844
16845
  return [3 /*break*/, 3];
16845
16846
  case 2:
@@ -16854,6 +16855,7 @@ function useCAMSMSALAuth(options) {
16854
16855
  return {
16855
16856
  login: login,
16856
16857
  logout: logout,
16858
+ storageKey: storageKey,
16857
16859
  isAuthenticated: isAuthenticated,
16858
16860
  isLoading: isLoading,
16859
16861
  error: error,
@@ -17317,7 +17319,7 @@ function ProtectedRoute(_a) {
17317
17319
  var CAMSMSALContext = createContext(null);
17318
17320
  var isTokenValid = function (token) {
17319
17321
  try {
17320
- var payload = JSON.parse(atob(token.split('.')[1]));
17322
+ var payload = JSON.parse(atob(token.split(".")[1]));
17321
17323
  return payload.exp * 1000 > Date.now();
17322
17324
  }
17323
17325
  catch (_a) {
@@ -17325,17 +17327,58 @@ var isTokenValid = function (token) {
17325
17327
  }
17326
17328
  };
17327
17329
  function CAMSMSALProviderInner(_a) {
17330
+ var _this = this;
17328
17331
  var children = _a.children, authOptions = __rest(_a, ["children"]);
17329
17332
  var auth = useCAMSMSALAuth(authOptions);
17333
+ var _b = useState(null), userProfile = _b[0], setUserProfile = _b[1];
17334
+ var profileStorageKey = "".concat(auth.storageKey, "-PROFILE");
17335
+ // Load profile from storage on mount
17330
17336
  useEffect(function () {
17331
- if (auth.accessToken && isTokenValid(auth.accessToken) && typeof window !== 'undefined') {
17332
- localStorage.setItem('cams-msal-auth', JSON.stringify({
17337
+ if (typeof window !== "undefined" && !userProfile) {
17338
+ var storedProfile = localStorage.getItem(profileStorageKey);
17339
+ if (storedProfile) {
17340
+ try {
17341
+ setUserProfile(JSON.parse(storedProfile));
17342
+ }
17343
+ catch (_a) { }
17344
+ }
17345
+ }
17346
+ }, [profileStorageKey, userProfile]);
17347
+ // Persist tokens and profile
17348
+ useEffect(function () {
17349
+ if (auth.accessToken &&
17350
+ isTokenValid(auth.accessToken) &&
17351
+ typeof window !== "undefined") {
17352
+ localStorage.setItem(auth.storageKey, JSON.stringify({
17333
17353
  accessToken: auth.accessToken,
17334
- idToken: auth.idToken
17354
+ idToken: auth.idToken,
17335
17355
  }));
17336
17356
  }
17337
- }, [auth.accessToken, auth.idToken]);
17338
- var value = useMemo(function () { return auth; }, [auth]);
17357
+ }, [auth.accessToken, auth.idToken, auth.storageKey]);
17358
+ // Persist profile separately
17359
+ useEffect(function () {
17360
+ if (typeof window !== "undefined") {
17361
+ if (userProfile) {
17362
+ localStorage.setItem(profileStorageKey, JSON.stringify(userProfile));
17363
+ }
17364
+ else {
17365
+ localStorage.removeItem(profileStorageKey);
17366
+ }
17367
+ }
17368
+ }, [userProfile, profileStorageKey]);
17369
+ // Enhanced logout that also clears profile
17370
+ var enhancedLogout = function () { return __awaiter(_this, void 0, void 0, function () {
17371
+ return __generator(this, function (_a) {
17372
+ switch (_a.label) {
17373
+ case 0: return [4 /*yield*/, auth.logout()];
17374
+ case 1:
17375
+ _a.sent();
17376
+ setUserProfile(null);
17377
+ return [2 /*return*/];
17378
+ }
17379
+ });
17380
+ }); };
17381
+ var value = useMemo(function () { return (__assign(__assign({}, auth), { logout: enhancedLogout, userProfile: userProfile, setUserProfile: setUserProfile })); }, [auth, userProfile]);
17339
17382
  return (jsxRuntimeExports.jsx(CAMSMSALContext.Provider, { value: value, children: children }));
17340
17383
  }
17341
17384
  function CAMSMSALProvider(props) {
@@ -17346,7 +17389,7 @@ function CAMSMSALProvider(props) {
17346
17389
  function useCAMSMSALContext() {
17347
17390
  var context = useContext(CAMSMSALContext);
17348
17391
  if (!context) {
17349
- throw new Error('useCAMSMSALContext must be used within a CAMSMSALProvider');
17392
+ throw new Error("useCAMSMSALContext must be used within a CAMSMSALProvider");
17350
17393
  }
17351
17394
  return context;
17352
17395
  }