@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.
@@ -1,13 +1,16 @@
1
- import React from 'react';
2
- import { PublicClientApplication, Configuration } from '@azure/msal-browser';
3
- import { UseCAMSMSALAuthReturn, UseCAMSMSALAuthOptions } from '../hooks/useCAMSMSALAuth';
1
+ import React from "react";
2
+ import { PublicClientApplication, Configuration } from "@azure/msal-browser";
3
+ import { Profile } from "@nibssplc/cams-sdk";
4
+ import { UseCAMSMSALAuthReturn, UseCAMSMSALAuthOptions } from "../hooks/useCAMSMSALAuth";
4
5
  interface CAMSMSALContextValue extends UseCAMSMSALAuthReturn {
6
+ userProfile: Profile | null;
7
+ setUserProfile: (profile: Profile | null) => void;
5
8
  }
6
9
  export interface CAMSMSALProviderProps extends UseCAMSMSALAuthOptions {
7
10
  children: React.ReactNode;
8
11
  msalConfig: Configuration;
9
12
  msalInstance?: PublicClientApplication;
10
13
  }
11
- export declare function CAMSMSALProvider(props: CAMSMSALProviderProps): import("react/jsx-runtime").JSX.Element;
14
+ export declare function CAMSMSALProvider(props: Readonly<CAMSMSALProviderProps>): import("react/jsx-runtime").JSX.Element;
12
15
  export declare function useCAMSMSALContext(): CAMSMSALContextValue;
13
16
  export {};
@@ -10,6 +10,7 @@ export interface UseCAMSMSALAuthOptions {
10
10
  export interface UseCAMSMSALAuthReturn {
11
11
  login: () => Promise<void>;
12
12
  logout: () => Promise<void>;
13
+ storageKey: string;
13
14
  isAuthenticated: boolean;
14
15
  isLoading: boolean;
15
16
  error: CAMSError | null;
package/dist/index.cjs.js CHANGED
@@ -16714,6 +16714,7 @@ function useCAMSMSALAuth(options) {
16714
16714
  }; }
16715
16715
  var _a = useMsal(), instance = _a.instance, inProgress = _a.inProgress, accounts = _a.accounts;
16716
16716
  var account = useAccount(accounts[0] || {});
16717
+ var storageKey = 'CAMS-MSAL-AUTH-SDK';
16717
16718
  var _b = React__default.useState(null), error = _b[0], setError = _b[1];
16718
16719
  var _c = React__default.useState(null), token = _c[0], setToken = _c[1];
16719
16720
  var _d = React__default.useState(null), idToken = _d[0], setIdToken = _d[1];
@@ -16732,7 +16733,7 @@ function useCAMSMSALAuth(options) {
16732
16733
  };
16733
16734
  React__default.useEffect(function () {
16734
16735
  if (typeof window !== 'undefined' && !token) {
16735
- var stored = localStorage.getItem('cams-msal-auth');
16736
+ var stored = localStorage.getItem(storageKey);
16736
16737
  if (stored) {
16737
16738
  try {
16738
16739
  var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken;
@@ -16742,7 +16743,7 @@ function useCAMSMSALAuth(options) {
16742
16743
  setIdToken(idToken_1);
16743
16744
  }
16744
16745
  else {
16745
- localStorage.removeItem('cams-msal-auth');
16746
+ localStorage.removeItem(storageKey);
16746
16747
  }
16747
16748
  }
16748
16749
  catch (_b) { }
@@ -16805,6 +16806,14 @@ function useCAMSMSALAuth(options) {
16805
16806
  setToken(response.accessToken);
16806
16807
  setAccessToken(response.accessToken);
16807
16808
  setIdToken(response.idToken);
16809
+ // Persist tokens to localStorage
16810
+ if (typeof window !== 'undefined') {
16811
+ localStorage.setItem(storageKey, JSON.stringify({
16812
+ isAuthenticated: true,
16813
+ accessToken: response.accessToken,
16814
+ idToken: response.idToken
16815
+ }));
16816
+ }
16808
16817
  (_a = options.onAuthSuccess) === null || _a === void 0 ? void 0 : _a.call(options, response.accessToken);
16809
16818
  if (typeof window !== "undefined" &&
16810
16819
  process.env.NODE_ENV !== "test") {
@@ -16850,7 +16859,7 @@ function useCAMSMSALAuth(options) {
16850
16859
  setIdToken(null);
16851
16860
  setError(null);
16852
16861
  if (typeof window !== 'undefined') {
16853
- localStorage.removeItem('cams-msal-auth');
16862
+ localStorage.removeItem(storageKey);
16854
16863
  }
16855
16864
  return [3 /*break*/, 3];
16856
16865
  case 2:
@@ -16865,6 +16874,7 @@ function useCAMSMSALAuth(options) {
16865
16874
  return {
16866
16875
  login: login,
16867
16876
  logout: logout,
16877
+ storageKey: storageKey,
16868
16878
  isAuthenticated: isAuthenticated,
16869
16879
  isLoading: isLoading,
16870
16880
  error: error,
@@ -17328,7 +17338,7 @@ function ProtectedRoute(_a) {
17328
17338
  var CAMSMSALContext = React__default.createContext(null);
17329
17339
  var isTokenValid = function (token) {
17330
17340
  try {
17331
- var payload = JSON.parse(atob(token.split('.')[1]));
17341
+ var payload = JSON.parse(atob(token.split(".")[1]));
17332
17342
  return payload.exp * 1000 > Date.now();
17333
17343
  }
17334
17344
  catch (_a) {
@@ -17336,17 +17346,58 @@ var isTokenValid = function (token) {
17336
17346
  }
17337
17347
  };
17338
17348
  function CAMSMSALProviderInner(_a) {
17349
+ var _this = this;
17339
17350
  var children = _a.children, authOptions = __rest(_a, ["children"]);
17340
17351
  var auth = useCAMSMSALAuth(authOptions);
17352
+ var _b = React__default.useState(null), userProfile = _b[0], setUserProfile = _b[1];
17353
+ var profileStorageKey = "".concat(auth.storageKey, "-PROFILE");
17354
+ // Load profile from storage on mount
17341
17355
  React__default.useEffect(function () {
17342
- if (auth.accessToken && isTokenValid(auth.accessToken) && typeof window !== 'undefined') {
17343
- localStorage.setItem('cams-msal-auth', JSON.stringify({
17356
+ if (typeof window !== "undefined" && !userProfile) {
17357
+ var storedProfile = localStorage.getItem(profileStorageKey);
17358
+ if (storedProfile) {
17359
+ try {
17360
+ setUserProfile(JSON.parse(storedProfile));
17361
+ }
17362
+ catch (_a) { }
17363
+ }
17364
+ }
17365
+ }, [profileStorageKey, userProfile]);
17366
+ // Persist tokens and profile
17367
+ React__default.useEffect(function () {
17368
+ if (auth.accessToken &&
17369
+ isTokenValid(auth.accessToken) &&
17370
+ typeof window !== "undefined") {
17371
+ localStorage.setItem(auth.storageKey, JSON.stringify({
17344
17372
  accessToken: auth.accessToken,
17345
- idToken: auth.idToken
17373
+ idToken: auth.idToken,
17346
17374
  }));
17347
17375
  }
17348
- }, [auth.accessToken, auth.idToken]);
17349
- var value = React__default.useMemo(function () { return auth; }, [auth]);
17376
+ }, [auth.accessToken, auth.idToken, auth.storageKey]);
17377
+ // Persist profile separately
17378
+ React__default.useEffect(function () {
17379
+ if (typeof window !== "undefined") {
17380
+ if (userProfile) {
17381
+ localStorage.setItem(profileStorageKey, JSON.stringify(userProfile));
17382
+ }
17383
+ else {
17384
+ localStorage.removeItem(profileStorageKey);
17385
+ }
17386
+ }
17387
+ }, [userProfile, profileStorageKey]);
17388
+ // Enhanced logout that also clears profile
17389
+ var enhancedLogout = function () { return __awaiter(_this, void 0, void 0, function () {
17390
+ return __generator(this, function (_a) {
17391
+ switch (_a.label) {
17392
+ case 0: return [4 /*yield*/, auth.logout()];
17393
+ case 1:
17394
+ _a.sent();
17395
+ setUserProfile(null);
17396
+ return [2 /*return*/];
17397
+ }
17398
+ });
17399
+ }); };
17400
+ var value = React__default.useMemo(function () { return (__assign(__assign({}, auth), { logout: enhancedLogout, userProfile: userProfile, setUserProfile: setUserProfile })); }, [auth, userProfile]);
17350
17401
  return (jsxRuntimeExports.jsx(CAMSMSALContext.Provider, { value: value, children: children }));
17351
17402
  }
17352
17403
  function CAMSMSALProvider(props) {
@@ -17357,7 +17408,7 @@ function CAMSMSALProvider(props) {
17357
17408
  function useCAMSMSALContext() {
17358
17409
  var context = React__default.useContext(CAMSMSALContext);
17359
17410
  if (!context) {
17360
- throw new Error('useCAMSMSALContext must be used within a CAMSMSALProvider');
17411
+ throw new Error("useCAMSMSALContext must be used within a CAMSMSALProvider");
17361
17412
  }
17362
17413
  return context;
17363
17414
  }