@nibssplc/cams-sdk-react 0.0.1-beta.48 → 0.0.1-beta.50

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
@@ -88,19 +88,22 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
88
88
 
89
89
  function useCAMSAuth(options) {
90
90
  var _this = this;
91
- if (options === void 0) { options = {}; }
91
+ if (options === void 0) { options = {
92
+ appCode: "",
93
+ storageKey: "CAMS-AUTH-SDK",
94
+ }; }
92
95
  var _a = useState(false), isAuthenticated = _a[0], setIsAuthenticated = _a[1];
93
96
  var _b = useState(false), isLoading = _b[0], setIsLoading = _b[1];
94
97
  var _c = useState(null), error = _c[0], setError = _c[1];
95
- var _d = useState(null), token = _d[0], setToken = _d[1];
98
+ var _d = useState(""), token = _d[0], setToken = _d[1];
96
99
  var _e = useState(null), profile = _e[0], setProfile = _e[1];
97
100
  var sessionManagerRef = useRef(null);
98
101
  useEffect(function () {
99
- var _a;
102
+ var _a, _b;
100
103
  // Only initialize on client side
101
- if (typeof window === 'undefined')
104
+ if (typeof window === "undefined")
102
105
  return;
103
- (_a = sessionManagerRef.current) !== null && _a !== void 0 ? _a : (sessionManagerRef.current = new CAMSSessionManager(localStorage, options.storageKey || 'CAMS-SDK', {
106
+ (_a = sessionManagerRef.current) !== null && _a !== void 0 ? _a : (sessionManagerRef.current = new CAMSSessionManager(localStorage, options.storageKey || "CAMS-SDK", {
104
107
  onAuthSuccess: function (res) {
105
108
  var _a;
106
109
  setToken(res.userProfile.tokens.Bearer);
@@ -115,16 +118,16 @@ function useCAMSAuth(options) {
115
118
  onTokenExpired: function () {
116
119
  var _a;
117
120
  setIsAuthenticated(false);
118
- setToken(null);
121
+ setToken("");
119
122
  setProfile(null);
120
123
  (_a = options.onTokenExpired) === null || _a === void 0 ? void 0 : _a.call(options);
121
- }
124
+ },
122
125
  }));
123
126
  // Check initial auth state
124
127
  var initialAuth = sessionManagerRef.current.isAuthenticated();
125
128
  setIsAuthenticated(initialAuth);
126
129
  if (initialAuth) {
127
- setToken(sessionManagerRef.current.getAccessToken());
130
+ setToken((_b = sessionManagerRef.current.getAccessToken()) !== null && _b !== void 0 ? _b : "");
128
131
  sessionManagerRef.current.getProfile().then(setProfile);
129
132
  }
130
133
  }, [options.storageKey]);
@@ -170,7 +173,7 @@ function useCAMSAuth(options) {
170
173
  case 1:
171
174
  _a.sent();
172
175
  setIsAuthenticated(false);
173
- setToken(null);
176
+ setToken("");
174
177
  setProfile(null);
175
178
  setError(null);
176
179
  return [2 /*return*/];
@@ -184,7 +187,9 @@ function useCAMSAuth(options) {
184
187
  isLoading: isLoading,
185
188
  error: error,
186
189
  token: token,
187
- profile: profile
190
+ profile: profile,
191
+ appCode: options.appCode,
192
+ storageKey: options.storageKey || "CAMS-SDK",
188
193
  };
189
194
  }
190
195
 
@@ -16692,17 +16697,16 @@ function useCAMSMSALAuth(options) {
16692
16697
  var _this = this;
16693
16698
  if (options === void 0) { options = {
16694
16699
  mfaUrl: "/auth/multi-factor",
16700
+ storageKey: "CAMS-MSAL-AUTH-SDK",
16695
16701
  }; }
16696
16702
  var _a = useMsal(), instance = _a.instance, inProgress = _a.inProgress, accounts = _a.accounts;
16697
16703
  var account = useAccount(accounts[0] || {});
16698
- var storageKey = "CAMS-MSAL-AUTH-SDK";
16699
16704
  var _b = useState(null), error = _b[0], setError = _b[1];
16700
- var _c = useState(""), token = _c[0], setToken = _c[1];
16701
- var _d = useState(""), idToken = _d[0], setIdToken = _d[1];
16702
- var _e = useState(""), appCode = _e[0], setAppCode = _e[1];
16703
- var _f = useState(""), accessToken = _f[0], setAccessToken = _f[1];
16705
+ var _c = useState(""), idToken = _c[0], setIdToken = _c[1];
16706
+ var _d = useState(""), appCode = _d[0], setAppCode = _d[1];
16707
+ var _e = useState(""), accessToken = _e[0], setAccessToken = _e[1];
16704
16708
  var isLoading = inProgress !== InteractionStatus.None;
16705
- var isAuthenticated = !!account && !!token;
16709
+ var isAuthenticated = !!account && !!accessToken;
16706
16710
  var scopes = options.scopes || ["openid", "profile", "email"];
16707
16711
  var isTokenValid = function (token) {
16708
16712
  try {
@@ -16714,25 +16718,24 @@ function useCAMSMSALAuth(options) {
16714
16718
  }
16715
16719
  };
16716
16720
  useEffect(function () {
16717
- if (typeof window !== "undefined" && !token) {
16718
- var stored = localStorage.getItem(storageKey);
16721
+ if (typeof window !== "undefined" && !accessToken) {
16722
+ var stored = localStorage.getItem(options.storageKey);
16719
16723
  if (stored) {
16720
16724
  try {
16721
16725
  var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken, appCode_1 = _a.appCode;
16722
16726
  if (accessToken_1 && isTokenValid(accessToken_1)) {
16723
- setToken(accessToken_1);
16724
16727
  setAccessToken(accessToken_1);
16725
16728
  setIdToken(idToken_1);
16726
16729
  setAppCode(appCode_1);
16727
16730
  }
16728
16731
  else {
16729
- localStorage.removeItem(storageKey);
16732
+ localStorage.removeItem(options.storageKey);
16730
16733
  }
16731
16734
  }
16732
16735
  catch (_b) { }
16733
16736
  }
16734
16737
  }
16735
- }, [token]);
16738
+ }, [accessToken]);
16736
16739
  // useEffect(() => {
16737
16740
  // const handleRedirect = async () => {
16738
16741
  // try {
@@ -16786,12 +16789,11 @@ function useCAMSMSALAuth(options) {
16786
16789
  accessToken: accessToken,
16787
16790
  idToken: idToken,
16788
16791
  });
16789
- setToken(response.accessToken);
16790
16792
  setAccessToken(response.accessToken);
16791
16793
  setIdToken(response.idToken);
16792
16794
  // Persist tokens to localStorage
16793
16795
  if (typeof window !== "undefined") {
16794
- localStorage.setItem(storageKey, JSON.stringify({
16796
+ localStorage.setItem(options.storageKey, JSON.stringify({
16795
16797
  isAuthenticated: true,
16796
16798
  accessToken: response.accessToken,
16797
16799
  idToken: response.idToken,
@@ -16838,12 +16840,16 @@ function useCAMSMSALAuth(options) {
16838
16840
  var listener_1 = function (event) {
16839
16841
  if (event.source !== authWindow_1)
16840
16842
  return;
16841
- var expectedOrigin = options.messageOrigin || new URL(options.mfaUrl || "").origin;
16842
- if (expectedOrigin && event.origin !== expectedOrigin)
16843
+ var allowedOrigins = options.allowedOrigins ||
16844
+ [
16845
+ options.messageOrigin || new URL(options.mfaUrl || "").origin,
16846
+ ].filter(Boolean);
16847
+ if (allowedOrigins.length > 0 &&
16848
+ !allowedOrigins.includes(event.origin))
16843
16849
  return;
16844
16850
  var tokenMsg = ProfileSchema.safeParse(event.data);
16845
16851
  if (tokenMsg.success) {
16846
- localStorage.setItem(storageKey, JSON.stringify({
16852
+ localStorage.setItem(options.storageKey, JSON.stringify({
16847
16853
  isAuthenticated: true,
16848
16854
  accessToken: response.accessToken,
16849
16855
  idToken: response.idToken,
@@ -16905,13 +16911,12 @@ function useCAMSMSALAuth(options) {
16905
16911
  return [4 /*yield*/, instance.logoutRedirect()];
16906
16912
  case 1:
16907
16913
  _a.sent();
16908
- setToken("");
16909
16914
  setAccessToken("");
16910
16915
  setIdToken("");
16911
16916
  setAppCode("");
16912
16917
  setError(null);
16913
16918
  if (typeof window !== "undefined") {
16914
- localStorage.removeItem(storageKey);
16919
+ localStorage.removeItem(options.storageKey);
16915
16920
  }
16916
16921
  return [3 /*break*/, 3];
16917
16922
  case 2:
@@ -16926,7 +16931,7 @@ function useCAMSMSALAuth(options) {
16926
16931
  return {
16927
16932
  login: login,
16928
16933
  logout: logout,
16929
- storageKey: storageKey,
16934
+ storageKey: options.storageKey,
16930
16935
  isAuthenticated: isAuthenticated,
16931
16936
  isLoading: isLoading,
16932
16937
  error: error,
@@ -17356,11 +17361,81 @@ if (process.env.NODE_ENV === 'production') {
17356
17361
  var jsxRuntimeExports = jsxRuntime.exports;
17357
17362
 
17358
17363
  var CAMSContext = createContext(null);
17364
+ var setCookie$1 = function (name, value, days) {
17365
+ var expires = new Date(Date.now() + days * 864e5).toUTCString();
17366
+ document.cookie = "".concat(name, "=").concat(encodeURIComponent(value), "; expires=").concat(expires, "; path=/; samesite=Lax");
17367
+ };
17368
+ var getCookie$1 = function (name) {
17369
+ var _a;
17370
+ return ((_a = document.cookie
17371
+ .split("; ")
17372
+ .find(function (row) { return row.startsWith(name + "="); })) === null || _a === void 0 ? void 0 : _a.split("=")[1])
17373
+ ? decodeURIComponent(document.cookie
17374
+ .split("; ")
17375
+ .find(function (row) { return row.startsWith(name + "="); })
17376
+ .split("=")[1])
17377
+ : null;
17378
+ };
17379
+ var deleteCookie$1 = function (name) {
17380
+ document.cookie = name + "=; Max-Age=-99999999; path=/";
17381
+ };
17359
17382
  function CAMSProvider(_a) {
17360
- var children = _a.children, defaultConfig = _a.defaultConfig, msalOptions = _a.msalOptions, authOptions = __rest(_a, ["children", "defaultConfig", "msalOptions"]);
17383
+ var _this = this;
17384
+ var children = _a.children, authOptions = __rest(_a, ["children"]);
17361
17385
  var auth = useCAMSAuth(authOptions);
17362
- var msalAuth = useCAMSMSALAuth(msalOptions || { mfaUrl: '/auth/multi-factor' });
17363
- var value = useMemo(function () { return (__assign(__assign({}, auth), { defaultConfig: defaultConfig, isAuthenticated: msalAuth.isAuthenticated, idToken: msalAuth.idToken, accessToken: msalAuth.accessToken })); }, [auth, defaultConfig, msalAuth.isAuthenticated, msalAuth.idToken, msalAuth.accessToken]);
17386
+ var profileStorageKey = "".concat(auth.storageKey, "-PROFILE");
17387
+ var getInitialProfile = function () {
17388
+ if (typeof window === "undefined") {
17389
+ return null;
17390
+ }
17391
+ try {
17392
+ var storedProfile = getCookie$1(profileStorageKey);
17393
+ return storedProfile ? JSON.parse(storedProfile) : null;
17394
+ }
17395
+ catch (_a) {
17396
+ return null;
17397
+ }
17398
+ };
17399
+ var _b = useState(getInitialProfile), userProfile = _b[0], setUserProfile = _b[1];
17400
+ // Load profile from storage on mount
17401
+ useEffect(function () {
17402
+ if (typeof window !== "undefined") {
17403
+ var storedProfile = getCookie$1(profileStorageKey);
17404
+ if (storedProfile) {
17405
+ try {
17406
+ setUserProfile(JSON.parse(storedProfile));
17407
+ }
17408
+ catch (_a) { }
17409
+ }
17410
+ }
17411
+ }, [profileStorageKey]);
17412
+ // Persist profile separately
17413
+ useEffect(function () {
17414
+ if (typeof window !== "undefined") {
17415
+ if (userProfile) {
17416
+ setCookie$1(profileStorageKey, JSON.stringify(userProfile), 1);
17417
+ }
17418
+ else {
17419
+ deleteCookie$1(profileStorageKey);
17420
+ }
17421
+ }
17422
+ }, [userProfile, profileStorageKey]);
17423
+ // Enhanced logout that also clears profile
17424
+ var enhancedLogout = function () { return __awaiter(_this, void 0, void 0, function () {
17425
+ return __generator(this, function (_a) {
17426
+ switch (_a.label) {
17427
+ case 0: return [4 /*yield*/, auth.logout()];
17428
+ case 1:
17429
+ _a.sent();
17430
+ setUserProfile(null);
17431
+ if (typeof window !== "undefined") {
17432
+ deleteCookie$1(profileStorageKey);
17433
+ }
17434
+ return [2 /*return*/];
17435
+ }
17436
+ });
17437
+ }); };
17438
+ var value = useMemo(function () { return (__assign(__assign({}, auth), { logout: enhancedLogout, userProfile: userProfile, setUserProfile: setUserProfile })); }, [auth, userProfile]);
17364
17439
  return (jsxRuntimeExports.jsx(CAMSContext.Provider, { value: value, children: children }));
17365
17440
  }
17366
17441
  function useCAMSContext() {