@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.cjs.js CHANGED
@@ -107,19 +107,22 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
107
107
 
108
108
  function useCAMSAuth(options) {
109
109
  var _this = this;
110
- if (options === void 0) { options = {}; }
110
+ if (options === void 0) { options = {
111
+ appCode: "",
112
+ storageKey: "CAMS-AUTH-SDK",
113
+ }; }
111
114
  var _a = React__default.useState(false), isAuthenticated = _a[0], setIsAuthenticated = _a[1];
112
115
  var _b = React__default.useState(false), isLoading = _b[0], setIsLoading = _b[1];
113
116
  var _c = React__default.useState(null), error = _c[0], setError = _c[1];
114
- var _d = React__default.useState(null), token = _d[0], setToken = _d[1];
117
+ var _d = React__default.useState(""), token = _d[0], setToken = _d[1];
115
118
  var _e = React__default.useState(null), profile = _e[0], setProfile = _e[1];
116
119
  var sessionManagerRef = React__default.useRef(null);
117
120
  React__default.useEffect(function () {
118
- var _a;
121
+ var _a, _b;
119
122
  // Only initialize on client side
120
- if (typeof window === 'undefined')
123
+ if (typeof window === "undefined")
121
124
  return;
122
- (_a = sessionManagerRef.current) !== null && _a !== void 0 ? _a : (sessionManagerRef.current = new camsSdk.CAMSSessionManager(localStorage, options.storageKey || 'CAMS-SDK', {
125
+ (_a = sessionManagerRef.current) !== null && _a !== void 0 ? _a : (sessionManagerRef.current = new camsSdk.CAMSSessionManager(localStorage, options.storageKey || "CAMS-SDK", {
123
126
  onAuthSuccess: function (res) {
124
127
  var _a;
125
128
  setToken(res.userProfile.tokens.Bearer);
@@ -134,16 +137,16 @@ function useCAMSAuth(options) {
134
137
  onTokenExpired: function () {
135
138
  var _a;
136
139
  setIsAuthenticated(false);
137
- setToken(null);
140
+ setToken("");
138
141
  setProfile(null);
139
142
  (_a = options.onTokenExpired) === null || _a === void 0 ? void 0 : _a.call(options);
140
- }
143
+ },
141
144
  }));
142
145
  // Check initial auth state
143
146
  var initialAuth = sessionManagerRef.current.isAuthenticated();
144
147
  setIsAuthenticated(initialAuth);
145
148
  if (initialAuth) {
146
- setToken(sessionManagerRef.current.getAccessToken());
149
+ setToken((_b = sessionManagerRef.current.getAccessToken()) !== null && _b !== void 0 ? _b : "");
147
150
  sessionManagerRef.current.getProfile().then(setProfile);
148
151
  }
149
152
  }, [options.storageKey]);
@@ -189,7 +192,7 @@ function useCAMSAuth(options) {
189
192
  case 1:
190
193
  _a.sent();
191
194
  setIsAuthenticated(false);
192
- setToken(null);
195
+ setToken("");
193
196
  setProfile(null);
194
197
  setError(null);
195
198
  return [2 /*return*/];
@@ -203,7 +206,9 @@ function useCAMSAuth(options) {
203
206
  isLoading: isLoading,
204
207
  error: error,
205
208
  token: token,
206
- profile: profile
209
+ profile: profile,
210
+ appCode: options.appCode,
211
+ storageKey: options.storageKey || "CAMS-SDK",
207
212
  };
208
213
  }
209
214
 
@@ -16711,17 +16716,16 @@ function useCAMSMSALAuth(options) {
16711
16716
  var _this = this;
16712
16717
  if (options === void 0) { options = {
16713
16718
  mfaUrl: "/auth/multi-factor",
16719
+ storageKey: "CAMS-MSAL-AUTH-SDK",
16714
16720
  }; }
16715
16721
  var _a = useMsal(), instance = _a.instance, inProgress = _a.inProgress, accounts = _a.accounts;
16716
16722
  var account = useAccount(accounts[0] || {});
16717
- var storageKey = "CAMS-MSAL-AUTH-SDK";
16718
16723
  var _b = React__default.useState(null), error = _b[0], setError = _b[1];
16719
- var _c = React__default.useState(""), token = _c[0], setToken = _c[1];
16720
- var _d = React__default.useState(""), idToken = _d[0], setIdToken = _d[1];
16721
- var _e = React__default.useState(""), appCode = _e[0], setAppCode = _e[1];
16722
- var _f = React__default.useState(""), accessToken = _f[0], setAccessToken = _f[1];
16724
+ var _c = React__default.useState(""), idToken = _c[0], setIdToken = _c[1];
16725
+ var _d = React__default.useState(""), appCode = _d[0], setAppCode = _d[1];
16726
+ var _e = React__default.useState(""), accessToken = _e[0], setAccessToken = _e[1];
16723
16727
  var isLoading = inProgress !== InteractionStatus.None;
16724
- var isAuthenticated = !!account && !!token;
16728
+ var isAuthenticated = !!account && !!accessToken;
16725
16729
  var scopes = options.scopes || ["openid", "profile", "email"];
16726
16730
  var isTokenValid = function (token) {
16727
16731
  try {
@@ -16733,25 +16737,24 @@ function useCAMSMSALAuth(options) {
16733
16737
  }
16734
16738
  };
16735
16739
  React__default.useEffect(function () {
16736
- if (typeof window !== "undefined" && !token) {
16737
- var stored = localStorage.getItem(storageKey);
16740
+ if (typeof window !== "undefined" && !accessToken) {
16741
+ var stored = localStorage.getItem(options.storageKey);
16738
16742
  if (stored) {
16739
16743
  try {
16740
16744
  var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken, appCode_1 = _a.appCode;
16741
16745
  if (accessToken_1 && isTokenValid(accessToken_1)) {
16742
- setToken(accessToken_1);
16743
16746
  setAccessToken(accessToken_1);
16744
16747
  setIdToken(idToken_1);
16745
16748
  setAppCode(appCode_1);
16746
16749
  }
16747
16750
  else {
16748
- localStorage.removeItem(storageKey);
16751
+ localStorage.removeItem(options.storageKey);
16749
16752
  }
16750
16753
  }
16751
16754
  catch (_b) { }
16752
16755
  }
16753
16756
  }
16754
- }, [token]);
16757
+ }, [accessToken]);
16755
16758
  // useEffect(() => {
16756
16759
  // const handleRedirect = async () => {
16757
16760
  // try {
@@ -16805,12 +16808,11 @@ function useCAMSMSALAuth(options) {
16805
16808
  accessToken: accessToken,
16806
16809
  idToken: idToken,
16807
16810
  });
16808
- setToken(response.accessToken);
16809
16811
  setAccessToken(response.accessToken);
16810
16812
  setIdToken(response.idToken);
16811
16813
  // Persist tokens to localStorage
16812
16814
  if (typeof window !== "undefined") {
16813
- localStorage.setItem(storageKey, JSON.stringify({
16815
+ localStorage.setItem(options.storageKey, JSON.stringify({
16814
16816
  isAuthenticated: true,
16815
16817
  accessToken: response.accessToken,
16816
16818
  idToken: response.idToken,
@@ -16857,12 +16859,16 @@ function useCAMSMSALAuth(options) {
16857
16859
  var listener_1 = function (event) {
16858
16860
  if (event.source !== authWindow_1)
16859
16861
  return;
16860
- var expectedOrigin = options.messageOrigin || new URL(options.mfaUrl || "").origin;
16861
- if (expectedOrigin && event.origin !== expectedOrigin)
16862
+ var allowedOrigins = options.allowedOrigins ||
16863
+ [
16864
+ options.messageOrigin || new URL(options.mfaUrl || "").origin,
16865
+ ].filter(Boolean);
16866
+ if (allowedOrigins.length > 0 &&
16867
+ !allowedOrigins.includes(event.origin))
16862
16868
  return;
16863
16869
  var tokenMsg = camsSdk.ProfileSchema.safeParse(event.data);
16864
16870
  if (tokenMsg.success) {
16865
- localStorage.setItem(storageKey, JSON.stringify({
16871
+ localStorage.setItem(options.storageKey, JSON.stringify({
16866
16872
  isAuthenticated: true,
16867
16873
  accessToken: response.accessToken,
16868
16874
  idToken: response.idToken,
@@ -16924,13 +16930,12 @@ function useCAMSMSALAuth(options) {
16924
16930
  return [4 /*yield*/, instance.logoutRedirect()];
16925
16931
  case 1:
16926
16932
  _a.sent();
16927
- setToken("");
16928
16933
  setAccessToken("");
16929
16934
  setIdToken("");
16930
16935
  setAppCode("");
16931
16936
  setError(null);
16932
16937
  if (typeof window !== "undefined") {
16933
- localStorage.removeItem(storageKey);
16938
+ localStorage.removeItem(options.storageKey);
16934
16939
  }
16935
16940
  return [3 /*break*/, 3];
16936
16941
  case 2:
@@ -16945,7 +16950,7 @@ function useCAMSMSALAuth(options) {
16945
16950
  return {
16946
16951
  login: login,
16947
16952
  logout: logout,
16948
- storageKey: storageKey,
16953
+ storageKey: options.storageKey,
16949
16954
  isAuthenticated: isAuthenticated,
16950
16955
  isLoading: isLoading,
16951
16956
  error: error,
@@ -17375,11 +17380,81 @@ if (process.env.NODE_ENV === 'production') {
17375
17380
  var jsxRuntimeExports = jsxRuntime.exports;
17376
17381
 
17377
17382
  var CAMSContext = React__default.createContext(null);
17383
+ var setCookie$1 = function (name, value, days) {
17384
+ var expires = new Date(Date.now() + days * 864e5).toUTCString();
17385
+ document.cookie = "".concat(name, "=").concat(encodeURIComponent(value), "; expires=").concat(expires, "; path=/; samesite=Lax");
17386
+ };
17387
+ var getCookie$1 = function (name) {
17388
+ var _a;
17389
+ return ((_a = document.cookie
17390
+ .split("; ")
17391
+ .find(function (row) { return row.startsWith(name + "="); })) === null || _a === void 0 ? void 0 : _a.split("=")[1])
17392
+ ? decodeURIComponent(document.cookie
17393
+ .split("; ")
17394
+ .find(function (row) { return row.startsWith(name + "="); })
17395
+ .split("=")[1])
17396
+ : null;
17397
+ };
17398
+ var deleteCookie$1 = function (name) {
17399
+ document.cookie = name + "=; Max-Age=-99999999; path=/";
17400
+ };
17378
17401
  function CAMSProvider(_a) {
17379
- var children = _a.children, defaultConfig = _a.defaultConfig, msalOptions = _a.msalOptions, authOptions = __rest(_a, ["children", "defaultConfig", "msalOptions"]);
17402
+ var _this = this;
17403
+ var children = _a.children, authOptions = __rest(_a, ["children"]);
17380
17404
  var auth = useCAMSAuth(authOptions);
17381
- var msalAuth = useCAMSMSALAuth(msalOptions || { mfaUrl: '/auth/multi-factor' });
17382
- var value = React__default.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]);
17405
+ var profileStorageKey = "".concat(auth.storageKey, "-PROFILE");
17406
+ var getInitialProfile = function () {
17407
+ if (typeof window === "undefined") {
17408
+ return null;
17409
+ }
17410
+ try {
17411
+ var storedProfile = getCookie$1(profileStorageKey);
17412
+ return storedProfile ? JSON.parse(storedProfile) : null;
17413
+ }
17414
+ catch (_a) {
17415
+ return null;
17416
+ }
17417
+ };
17418
+ var _b = React__default.useState(getInitialProfile), userProfile = _b[0], setUserProfile = _b[1];
17419
+ // Load profile from storage on mount
17420
+ React__default.useEffect(function () {
17421
+ if (typeof window !== "undefined") {
17422
+ var storedProfile = getCookie$1(profileStorageKey);
17423
+ if (storedProfile) {
17424
+ try {
17425
+ setUserProfile(JSON.parse(storedProfile));
17426
+ }
17427
+ catch (_a) { }
17428
+ }
17429
+ }
17430
+ }, [profileStorageKey]);
17431
+ // Persist profile separately
17432
+ React__default.useEffect(function () {
17433
+ if (typeof window !== "undefined") {
17434
+ if (userProfile) {
17435
+ setCookie$1(profileStorageKey, JSON.stringify(userProfile), 1);
17436
+ }
17437
+ else {
17438
+ deleteCookie$1(profileStorageKey);
17439
+ }
17440
+ }
17441
+ }, [userProfile, profileStorageKey]);
17442
+ // Enhanced logout that also clears profile
17443
+ var enhancedLogout = function () { return __awaiter(_this, void 0, void 0, function () {
17444
+ return __generator(this, function (_a) {
17445
+ switch (_a.label) {
17446
+ case 0: return [4 /*yield*/, auth.logout()];
17447
+ case 1:
17448
+ _a.sent();
17449
+ setUserProfile(null);
17450
+ if (typeof window !== "undefined") {
17451
+ deleteCookie$1(profileStorageKey);
17452
+ }
17453
+ return [2 /*return*/];
17454
+ }
17455
+ });
17456
+ }); };
17457
+ var value = React__default.useMemo(function () { return (__assign(__assign({}, auth), { logout: enhancedLogout, userProfile: userProfile, setUserProfile: setUserProfile })); }, [auth, userProfile]);
17383
17458
  return (jsxRuntimeExports.jsx(CAMSContext.Provider, { value: value, children: children }));
17384
17459
  }
17385
17460
  function useCAMSContext() {