@nibssplc/cams-sdk-react 0.0.1-beta.35 → 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) { }
@@ -16786,6 +16787,14 @@ function useCAMSMSALAuth(options) {
16786
16787
  setToken(response.accessToken);
16787
16788
  setAccessToken(response.accessToken);
16788
16789
  setIdToken(response.idToken);
16790
+ // Persist tokens to localStorage
16791
+ if (typeof window !== 'undefined') {
16792
+ localStorage.setItem(storageKey, JSON.stringify({
16793
+ isAuthenticated: true,
16794
+ accessToken: response.accessToken,
16795
+ idToken: response.idToken
16796
+ }));
16797
+ }
16789
16798
  (_a = options.onAuthSuccess) === null || _a === void 0 ? void 0 : _a.call(options, response.accessToken);
16790
16799
  if (typeof window !== "undefined" &&
16791
16800
  process.env.NODE_ENV !== "test") {
@@ -16831,7 +16840,7 @@ function useCAMSMSALAuth(options) {
16831
16840
  setIdToken(null);
16832
16841
  setError(null);
16833
16842
  if (typeof window !== 'undefined') {
16834
- localStorage.removeItem('cams-msal-auth');
16843
+ localStorage.removeItem(storageKey);
16835
16844
  }
16836
16845
  return [3 /*break*/, 3];
16837
16846
  case 2:
@@ -16846,6 +16855,7 @@ function useCAMSMSALAuth(options) {
16846
16855
  return {
16847
16856
  login: login,
16848
16857
  logout: logout,
16858
+ storageKey: storageKey,
16849
16859
  isAuthenticated: isAuthenticated,
16850
16860
  isLoading: isLoading,
16851
16861
  error: error,
@@ -17309,7 +17319,7 @@ function ProtectedRoute(_a) {
17309
17319
  var CAMSMSALContext = createContext(null);
17310
17320
  var isTokenValid = function (token) {
17311
17321
  try {
17312
- var payload = JSON.parse(atob(token.split('.')[1]));
17322
+ var payload = JSON.parse(atob(token.split(".")[1]));
17313
17323
  return payload.exp * 1000 > Date.now();
17314
17324
  }
17315
17325
  catch (_a) {
@@ -17317,17 +17327,58 @@ var isTokenValid = function (token) {
17317
17327
  }
17318
17328
  };
17319
17329
  function CAMSMSALProviderInner(_a) {
17330
+ var _this = this;
17320
17331
  var children = _a.children, authOptions = __rest(_a, ["children"]);
17321
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
17322
17336
  useEffect(function () {
17323
- if (auth.accessToken && isTokenValid(auth.accessToken) && typeof window !== 'undefined') {
17324
- 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({
17325
17353
  accessToken: auth.accessToken,
17326
- idToken: auth.idToken
17354
+ idToken: auth.idToken,
17327
17355
  }));
17328
17356
  }
17329
- }, [auth.accessToken, auth.idToken]);
17330
- 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]);
17331
17382
  return (jsxRuntimeExports.jsx(CAMSMSALContext.Provider, { value: value, children: children }));
17332
17383
  }
17333
17384
  function CAMSMSALProvider(props) {
@@ -17338,7 +17389,7 @@ function CAMSMSALProvider(props) {
17338
17389
  function useCAMSMSALContext() {
17339
17390
  var context = useContext(CAMSMSALContext);
17340
17391
  if (!context) {
17341
- throw new Error('useCAMSMSALContext must be used within a CAMSMSALProvider');
17392
+ throw new Error("useCAMSMSALContext must be used within a CAMSMSALProvider");
17342
17393
  }
17343
17394
  return context;
17344
17395
  }