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

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) { }
@@ -16807,7 +16808,7 @@ function useCAMSMSALAuth(options) {
16807
16808
  setIdToken(response.idToken);
16808
16809
  // Persist tokens to localStorage
16809
16810
  if (typeof window !== 'undefined') {
16810
- localStorage.setItem('cams-msal-auth', JSON.stringify({
16811
+ localStorage.setItem(storageKey, JSON.stringify({
16811
16812
  isAuthenticated: true,
16812
16813
  accessToken: response.accessToken,
16813
16814
  idToken: response.idToken
@@ -16858,7 +16859,7 @@ function useCAMSMSALAuth(options) {
16858
16859
  setIdToken(null);
16859
16860
  setError(null);
16860
16861
  if (typeof window !== 'undefined') {
16861
- localStorage.removeItem('cams-msal-auth');
16862
+ localStorage.removeItem(storageKey);
16862
16863
  }
16863
16864
  return [3 /*break*/, 3];
16864
16865
  case 2:
@@ -16873,6 +16874,7 @@ function useCAMSMSALAuth(options) {
16873
16874
  return {
16874
16875
  login: login,
16875
16876
  logout: logout,
16877
+ storageKey: storageKey,
16876
16878
  isAuthenticated: isAuthenticated,
16877
16879
  isLoading: isLoading,
16878
16880
  error: error,
@@ -17336,7 +17338,7 @@ function ProtectedRoute(_a) {
17336
17338
  var CAMSMSALContext = React__default.createContext(null);
17337
17339
  var isTokenValid = function (token) {
17338
17340
  try {
17339
- var payload = JSON.parse(atob(token.split('.')[1]));
17341
+ var payload = JSON.parse(atob(token.split(".")[1]));
17340
17342
  return payload.exp * 1000 > Date.now();
17341
17343
  }
17342
17344
  catch (_a) {
@@ -17344,17 +17346,70 @@ var isTokenValid = function (token) {
17344
17346
  }
17345
17347
  };
17346
17348
  function CAMSMSALProviderInner(_a) {
17349
+ var _this = this;
17347
17350
  var children = _a.children, authOptions = __rest(_a, ["children"]);
17348
17351
  var auth = useCAMSMSALAuth(authOptions);
17352
+ var getInitialProfile = function () {
17353
+ if (typeof window === "undefined") {
17354
+ return null;
17355
+ }
17356
+ try {
17357
+ var storedProfile = localStorage.getItem(profileStorageKey);
17358
+ return storedProfile ? JSON.parse(storedProfile) : null;
17359
+ }
17360
+ catch (_a) {
17361
+ return null;
17362
+ }
17363
+ };
17364
+ var _b = React__default.useState(getInitialProfile), userProfile = _b[0], setUserProfile = _b[1];
17365
+ var profileStorageKey = "".concat(auth.storageKey, "-PROFILE");
17366
+ // Load profile from storage on mount
17349
17367
  React__default.useEffect(function () {
17350
- if (auth.accessToken && isTokenValid(auth.accessToken) && typeof window !== 'undefined') {
17351
- localStorage.setItem('cams-msal-auth', JSON.stringify({
17368
+ if (typeof window !== "undefined") {
17369
+ var storedProfile = localStorage.getItem(profileStorageKey);
17370
+ if (storedProfile) {
17371
+ try {
17372
+ setUserProfile(JSON.parse(storedProfile));
17373
+ }
17374
+ catch (_a) { }
17375
+ }
17376
+ }
17377
+ }, [profileStorageKey]);
17378
+ // Persist tokens and profile
17379
+ React__default.useEffect(function () {
17380
+ if (auth.accessToken &&
17381
+ isTokenValid(auth.accessToken) &&
17382
+ typeof window !== "undefined") {
17383
+ localStorage.setItem(auth.storageKey, JSON.stringify({
17352
17384
  accessToken: auth.accessToken,
17353
- idToken: auth.idToken
17385
+ idToken: auth.idToken,
17354
17386
  }));
17355
17387
  }
17356
- }, [auth.accessToken, auth.idToken]);
17357
- var value = React__default.useMemo(function () { return auth; }, [auth]);
17388
+ }, [auth.accessToken, auth.idToken, auth.storageKey]);
17389
+ // Persist profile separately
17390
+ React__default.useEffect(function () {
17391
+ if (typeof window !== "undefined") {
17392
+ if (userProfile) {
17393
+ localStorage.setItem(profileStorageKey, JSON.stringify(userProfile));
17394
+ }
17395
+ else {
17396
+ localStorage.removeItem(profileStorageKey);
17397
+ }
17398
+ }
17399
+ }, [userProfile, profileStorageKey]);
17400
+ // Enhanced logout that also clears profile
17401
+ var enhancedLogout = function () { return __awaiter(_this, void 0, void 0, function () {
17402
+ return __generator(this, function (_a) {
17403
+ switch (_a.label) {
17404
+ case 0: return [4 /*yield*/, auth.logout()];
17405
+ case 1:
17406
+ _a.sent();
17407
+ setUserProfile(null);
17408
+ return [2 /*return*/];
17409
+ }
17410
+ });
17411
+ }); };
17412
+ var value = React__default.useMemo(function () { return (__assign(__assign({}, auth), { logout: enhancedLogout, userProfile: userProfile, setUserProfile: setUserProfile })); }, [auth, userProfile]);
17358
17413
  return (jsxRuntimeExports.jsx(CAMSMSALContext.Provider, { value: value, children: children }));
17359
17414
  }
17360
17415
  function CAMSMSALProvider(props) {
@@ -17365,7 +17420,7 @@ function CAMSMSALProvider(props) {
17365
17420
  function useCAMSMSALContext() {
17366
17421
  var context = React__default.useContext(CAMSMSALContext);
17367
17422
  if (!context) {
17368
- throw new Error('useCAMSMSALContext must be used within a CAMSMSALProvider');
17423
+ throw new Error("useCAMSMSALContext must be used within a CAMSMSALProvider");
17369
17424
  }
17370
17425
  return context;
17371
17426
  }