@nibssplc/cams-sdk-react 0.0.1-beta.49 → 0.0.1-beta.51

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,70 +107,84 @@ 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);
126
129
  setIsAuthenticated(true);
130
+ setIsLoading(false);
127
131
  (_a = options.onAuthSuccess) === null || _a === void 0 ? void 0 : _a.call(options, res.userProfile.tokens.Bearer);
128
132
  },
129
133
  onAuthError: function (error) {
130
134
  var _a;
131
135
  setError(error);
136
+ setIsAuthenticated(false);
137
+ setToken("");
138
+ setProfile(null);
139
+ setIsLoading(false);
132
140
  (_a = options.onAuthError) === null || _a === void 0 ? void 0 : _a.call(options, error);
133
141
  },
134
142
  onTokenExpired: function () {
135
143
  var _a;
136
144
  setIsAuthenticated(false);
137
- setToken(null);
145
+ setToken("");
138
146
  setProfile(null);
139
147
  (_a = options.onTokenExpired) === null || _a === void 0 ? void 0 : _a.call(options);
140
- }
148
+ },
141
149
  }));
142
150
  // Check initial auth state
143
151
  var initialAuth = sessionManagerRef.current.isAuthenticated();
144
152
  setIsAuthenticated(initialAuth);
145
153
  if (initialAuth) {
146
- setToken(sessionManagerRef.current.getAccessToken());
154
+ setToken((_b = sessionManagerRef.current.getAccessToken()) !== null && _b !== void 0 ? _b : "");
147
155
  sessionManagerRef.current.getProfile().then(setProfile);
148
156
  }
149
157
  }, [options.storageKey]);
150
158
  var login = React__default.useCallback(function (config) { return __awaiter(_this, void 0, void 0, function () {
151
- var userProfile, err_1;
152
- return __generator(this, function (_a) {
153
- switch (_a.label) {
159
+ var loginConfig, userProfile, err_1;
160
+ var _a;
161
+ return __generator(this, function (_b) {
162
+ switch (_b.label) {
154
163
  case 0:
155
164
  if (!sessionManagerRef.current)
156
165
  return [2 /*return*/];
157
166
  setIsLoading(true);
158
167
  setError(null);
159
- _a.label = 1;
168
+ _b.label = 1;
160
169
  case 1:
161
- _a.trys.push([1, 4, 5, 6]);
162
- return [4 /*yield*/, sessionManagerRef.current.login(config)];
170
+ _b.trys.push([1, 4, 5, 6]);
171
+ loginConfig = __assign(__assign({}, config), {
172
+ // autoClose: options.autoClose ?? config.autoClose,
173
+ idleTimeout: (_a = options.idleTimeout) !== null && _a !== void 0 ? _a : config.idleTimeout });
174
+ return [4 /*yield*/, sessionManagerRef.current.login(loginConfig)];
163
175
  case 2:
164
- _a.sent();
176
+ _b.sent();
165
177
  return [4 /*yield*/, sessionManagerRef.current.getProfile()];
166
178
  case 3:
167
- userProfile = _a.sent();
179
+ userProfile = _b.sent();
168
180
  setProfile(userProfile);
169
181
  return [3 /*break*/, 6];
170
182
  case 4:
171
- err_1 = _a.sent();
183
+ err_1 = _b.sent();
172
184
  setError(err_1);
173
185
  setIsAuthenticated(false);
186
+ setToken("");
187
+ setProfile(null);
174
188
  return [3 /*break*/, 6];
175
189
  case 5:
176
190
  setIsLoading(false);
@@ -178,7 +192,7 @@ function useCAMSAuth(options) {
178
192
  case 6: return [2 /*return*/];
179
193
  }
180
194
  });
181
- }); }, []);
195
+ }); }, [options.idleTimeout]);
182
196
  var logout = React__default.useCallback(function () { return __awaiter(_this, void 0, void 0, function () {
183
197
  return __generator(this, function (_a) {
184
198
  switch (_a.label) {
@@ -189,7 +203,7 @@ function useCAMSAuth(options) {
189
203
  case 1:
190
204
  _a.sent();
191
205
  setIsAuthenticated(false);
192
- setToken(null);
206
+ setToken("");
193
207
  setProfile(null);
194
208
  setError(null);
195
209
  return [2 /*return*/];
@@ -203,7 +217,9 @@ function useCAMSAuth(options) {
203
217
  isLoading: isLoading,
204
218
  error: error,
205
219
  token: token,
206
- profile: profile
220
+ profile: profile,
221
+ appCode: options.appCode,
222
+ storageKey: options.storageKey || "CAMS-SDK",
207
223
  };
208
224
  }
209
225
 
@@ -16711,17 +16727,16 @@ function useCAMSMSALAuth(options) {
16711
16727
  var _this = this;
16712
16728
  if (options === void 0) { options = {
16713
16729
  mfaUrl: "/auth/multi-factor",
16730
+ storageKey: "CAMS-MSAL-AUTH-SDK",
16714
16731
  }; }
16715
16732
  var _a = useMsal(), instance = _a.instance, inProgress = _a.inProgress, accounts = _a.accounts;
16716
16733
  var account = useAccount(accounts[0] || {});
16717
- var storageKey = "CAMS-MSAL-AUTH-SDK";
16718
16734
  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];
16735
+ var _c = React__default.useState(""), idToken = _c[0], setIdToken = _c[1];
16736
+ var _d = React__default.useState(""), appCode = _d[0], setAppCode = _d[1];
16737
+ var _e = React__default.useState(""), accessToken = _e[0], setAccessToken = _e[1];
16723
16738
  var isLoading = inProgress !== InteractionStatus.None;
16724
- var isAuthenticated = !!account && !!token;
16739
+ var isAuthenticated = !!account && !!accessToken;
16725
16740
  var scopes = options.scopes || ["openid", "profile", "email"];
16726
16741
  var isTokenValid = function (token) {
16727
16742
  try {
@@ -16733,25 +16748,24 @@ function useCAMSMSALAuth(options) {
16733
16748
  }
16734
16749
  };
16735
16750
  React__default.useEffect(function () {
16736
- if (typeof window !== "undefined" && !token) {
16737
- var stored = localStorage.getItem(storageKey);
16751
+ if (typeof window !== "undefined" && !accessToken) {
16752
+ var stored = localStorage.getItem(options.storageKey);
16738
16753
  if (stored) {
16739
16754
  try {
16740
16755
  var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken, appCode_1 = _a.appCode;
16741
16756
  if (accessToken_1 && isTokenValid(accessToken_1)) {
16742
- setToken(accessToken_1);
16743
16757
  setAccessToken(accessToken_1);
16744
16758
  setIdToken(idToken_1);
16745
16759
  setAppCode(appCode_1);
16746
16760
  }
16747
16761
  else {
16748
- localStorage.removeItem(storageKey);
16762
+ localStorage.removeItem(options.storageKey);
16749
16763
  }
16750
16764
  }
16751
16765
  catch (_b) { }
16752
16766
  }
16753
16767
  }
16754
- }, [token]);
16768
+ }, [accessToken]);
16755
16769
  // useEffect(() => {
16756
16770
  // const handleRedirect = async () => {
16757
16771
  // try {
@@ -16805,12 +16819,11 @@ function useCAMSMSALAuth(options) {
16805
16819
  accessToken: accessToken,
16806
16820
  idToken: idToken,
16807
16821
  });
16808
- setToken(response.accessToken);
16809
16822
  setAccessToken(response.accessToken);
16810
16823
  setIdToken(response.idToken);
16811
16824
  // Persist tokens to localStorage
16812
16825
  if (typeof window !== "undefined") {
16813
- localStorage.setItem(storageKey, JSON.stringify({
16826
+ localStorage.setItem(options.storageKey, JSON.stringify({
16814
16827
  isAuthenticated: true,
16815
16828
  accessToken: response.accessToken,
16816
16829
  idToken: response.idToken,
@@ -16857,14 +16870,16 @@ function useCAMSMSALAuth(options) {
16857
16870
  var listener_1 = function (event) {
16858
16871
  if (event.source !== authWindow_1)
16859
16872
  return;
16860
- var allowedOrigins = options.allowedOrigins || [
16861
- options.messageOrigin || new URL(options.mfaUrl || "").origin
16862
- ].filter(Boolean);
16863
- if (allowedOrigins.length > 0 && !allowedOrigins.includes(event.origin))
16873
+ var allowedOrigins = options.allowedOrigins ||
16874
+ [
16875
+ options.messageOrigin || new URL(options.mfaUrl || "").origin,
16876
+ ].filter(Boolean);
16877
+ if (allowedOrigins.length > 0 &&
16878
+ !allowedOrigins.includes(event.origin))
16864
16879
  return;
16865
16880
  var tokenMsg = camsSdk.ProfileSchema.safeParse(event.data);
16866
16881
  if (tokenMsg.success) {
16867
- localStorage.setItem(storageKey, JSON.stringify({
16882
+ localStorage.setItem(options.storageKey, JSON.stringify({
16868
16883
  isAuthenticated: true,
16869
16884
  accessToken: response.accessToken,
16870
16885
  idToken: response.idToken,
@@ -16926,13 +16941,12 @@ function useCAMSMSALAuth(options) {
16926
16941
  return [4 /*yield*/, instance.logoutRedirect()];
16927
16942
  case 1:
16928
16943
  _a.sent();
16929
- setToken("");
16930
16944
  setAccessToken("");
16931
16945
  setIdToken("");
16932
16946
  setAppCode("");
16933
16947
  setError(null);
16934
16948
  if (typeof window !== "undefined") {
16935
- localStorage.removeItem(storageKey);
16949
+ localStorage.removeItem(options.storageKey);
16936
16950
  }
16937
16951
  return [3 /*break*/, 3];
16938
16952
  case 2:
@@ -16947,7 +16961,7 @@ function useCAMSMSALAuth(options) {
16947
16961
  return {
16948
16962
  login: login,
16949
16963
  logout: logout,
16950
- storageKey: storageKey,
16964
+ storageKey: options.storageKey,
16951
16965
  isAuthenticated: isAuthenticated,
16952
16966
  isLoading: isLoading,
16953
16967
  error: error,
@@ -17377,11 +17391,81 @@ if (process.env.NODE_ENV === 'production') {
17377
17391
  var jsxRuntimeExports = jsxRuntime.exports;
17378
17392
 
17379
17393
  var CAMSContext = React__default.createContext(null);
17394
+ var setCookie$1 = function (name, value, days) {
17395
+ var expires = new Date(Date.now() + days * 864e5).toUTCString();
17396
+ document.cookie = "".concat(name, "=").concat(encodeURIComponent(value), "; expires=").concat(expires, "; path=/; samesite=Lax");
17397
+ };
17398
+ var getCookie$1 = function (name) {
17399
+ var _a;
17400
+ return ((_a = document.cookie
17401
+ .split("; ")
17402
+ .find(function (row) { return row.startsWith(name + "="); })) === null || _a === void 0 ? void 0 : _a.split("=")[1])
17403
+ ? decodeURIComponent(document.cookie
17404
+ .split("; ")
17405
+ .find(function (row) { return row.startsWith(name + "="); })
17406
+ .split("=")[1])
17407
+ : null;
17408
+ };
17409
+ var deleteCookie$1 = function (name) {
17410
+ document.cookie = name + "=; Max-Age=-99999999; path=/";
17411
+ };
17380
17412
  function CAMSProvider(_a) {
17381
- var children = _a.children, defaultConfig = _a.defaultConfig, msalOptions = _a.msalOptions, authOptions = __rest(_a, ["children", "defaultConfig", "msalOptions"]);
17413
+ var _this = this;
17414
+ var children = _a.children, authOptions = __rest(_a, ["children"]);
17382
17415
  var auth = useCAMSAuth(authOptions);
17383
- var msalAuth = useCAMSMSALAuth(msalOptions || { mfaUrl: '/auth/multi-factor' });
17384
- 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]);
17416
+ var profileStorageKey = "".concat(auth.storageKey, "-PROFILE");
17417
+ var getInitialProfile = function () {
17418
+ if (typeof window === "undefined") {
17419
+ return null;
17420
+ }
17421
+ try {
17422
+ var storedProfile = getCookie$1(profileStorageKey);
17423
+ return storedProfile ? JSON.parse(storedProfile) : null;
17424
+ }
17425
+ catch (_a) {
17426
+ return null;
17427
+ }
17428
+ };
17429
+ var _b = React__default.useState(getInitialProfile), userProfile = _b[0], setUserProfile = _b[1];
17430
+ // Load profile from storage on mount
17431
+ React__default.useEffect(function () {
17432
+ if (typeof window !== "undefined") {
17433
+ var storedProfile = getCookie$1(profileStorageKey);
17434
+ if (storedProfile) {
17435
+ try {
17436
+ setUserProfile(JSON.parse(storedProfile));
17437
+ }
17438
+ catch (_a) { }
17439
+ }
17440
+ }
17441
+ }, [profileStorageKey]);
17442
+ // Persist profile separately
17443
+ React__default.useEffect(function () {
17444
+ if (typeof window !== "undefined") {
17445
+ if (userProfile) {
17446
+ setCookie$1(profileStorageKey, JSON.stringify(userProfile), 1);
17447
+ }
17448
+ else {
17449
+ deleteCookie$1(profileStorageKey);
17450
+ }
17451
+ }
17452
+ }, [userProfile, profileStorageKey]);
17453
+ // Enhanced logout that also clears profile
17454
+ var enhancedLogout = function () { return __awaiter(_this, void 0, void 0, function () {
17455
+ return __generator(this, function (_a) {
17456
+ switch (_a.label) {
17457
+ case 0: return [4 /*yield*/, auth.logout()];
17458
+ case 1:
17459
+ _a.sent();
17460
+ setUserProfile(null);
17461
+ if (typeof window !== "undefined") {
17462
+ deleteCookie$1(profileStorageKey);
17463
+ }
17464
+ return [2 /*return*/];
17465
+ }
17466
+ });
17467
+ }); };
17468
+ var value = React__default.useMemo(function () { return (__assign(__assign({}, auth), { logout: enhancedLogout, userProfile: userProfile, setUserProfile: setUserProfile })); }, [auth, userProfile]);
17385
17469
  return (jsxRuntimeExports.jsx(CAMSContext.Provider, { value: value, children: children }));
17386
17470
  }
17387
17471
  function useCAMSContext() {