@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.esm.js CHANGED
@@ -88,70 +88,84 @@ 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);
107
110
  setIsAuthenticated(true);
111
+ setIsLoading(false);
108
112
  (_a = options.onAuthSuccess) === null || _a === void 0 ? void 0 : _a.call(options, res.userProfile.tokens.Bearer);
109
113
  },
110
114
  onAuthError: function (error) {
111
115
  var _a;
112
116
  setError(error);
117
+ setIsAuthenticated(false);
118
+ setToken("");
119
+ setProfile(null);
120
+ setIsLoading(false);
113
121
  (_a = options.onAuthError) === null || _a === void 0 ? void 0 : _a.call(options, error);
114
122
  },
115
123
  onTokenExpired: function () {
116
124
  var _a;
117
125
  setIsAuthenticated(false);
118
- setToken(null);
126
+ setToken("");
119
127
  setProfile(null);
120
128
  (_a = options.onTokenExpired) === null || _a === void 0 ? void 0 : _a.call(options);
121
- }
129
+ },
122
130
  }));
123
131
  // Check initial auth state
124
132
  var initialAuth = sessionManagerRef.current.isAuthenticated();
125
133
  setIsAuthenticated(initialAuth);
126
134
  if (initialAuth) {
127
- setToken(sessionManagerRef.current.getAccessToken());
135
+ setToken((_b = sessionManagerRef.current.getAccessToken()) !== null && _b !== void 0 ? _b : "");
128
136
  sessionManagerRef.current.getProfile().then(setProfile);
129
137
  }
130
138
  }, [options.storageKey]);
131
139
  var login = useCallback(function (config) { return __awaiter(_this, void 0, void 0, function () {
132
- var userProfile, err_1;
133
- return __generator(this, function (_a) {
134
- switch (_a.label) {
140
+ var loginConfig, userProfile, err_1;
141
+ var _a;
142
+ return __generator(this, function (_b) {
143
+ switch (_b.label) {
135
144
  case 0:
136
145
  if (!sessionManagerRef.current)
137
146
  return [2 /*return*/];
138
147
  setIsLoading(true);
139
148
  setError(null);
140
- _a.label = 1;
149
+ _b.label = 1;
141
150
  case 1:
142
- _a.trys.push([1, 4, 5, 6]);
143
- return [4 /*yield*/, sessionManagerRef.current.login(config)];
151
+ _b.trys.push([1, 4, 5, 6]);
152
+ loginConfig = __assign(__assign({}, config), {
153
+ // autoClose: options.autoClose ?? config.autoClose,
154
+ idleTimeout: (_a = options.idleTimeout) !== null && _a !== void 0 ? _a : config.idleTimeout });
155
+ return [4 /*yield*/, sessionManagerRef.current.login(loginConfig)];
144
156
  case 2:
145
- _a.sent();
157
+ _b.sent();
146
158
  return [4 /*yield*/, sessionManagerRef.current.getProfile()];
147
159
  case 3:
148
- userProfile = _a.sent();
160
+ userProfile = _b.sent();
149
161
  setProfile(userProfile);
150
162
  return [3 /*break*/, 6];
151
163
  case 4:
152
- err_1 = _a.sent();
164
+ err_1 = _b.sent();
153
165
  setError(err_1);
154
166
  setIsAuthenticated(false);
167
+ setToken("");
168
+ setProfile(null);
155
169
  return [3 /*break*/, 6];
156
170
  case 5:
157
171
  setIsLoading(false);
@@ -159,7 +173,7 @@ function useCAMSAuth(options) {
159
173
  case 6: return [2 /*return*/];
160
174
  }
161
175
  });
162
- }); }, []);
176
+ }); }, [options.idleTimeout]);
163
177
  var logout = useCallback(function () { return __awaiter(_this, void 0, void 0, function () {
164
178
  return __generator(this, function (_a) {
165
179
  switch (_a.label) {
@@ -170,7 +184,7 @@ function useCAMSAuth(options) {
170
184
  case 1:
171
185
  _a.sent();
172
186
  setIsAuthenticated(false);
173
- setToken(null);
187
+ setToken("");
174
188
  setProfile(null);
175
189
  setError(null);
176
190
  return [2 /*return*/];
@@ -184,7 +198,9 @@ function useCAMSAuth(options) {
184
198
  isLoading: isLoading,
185
199
  error: error,
186
200
  token: token,
187
- profile: profile
201
+ profile: profile,
202
+ appCode: options.appCode,
203
+ storageKey: options.storageKey || "CAMS-SDK",
188
204
  };
189
205
  }
190
206
 
@@ -16692,17 +16708,16 @@ function useCAMSMSALAuth(options) {
16692
16708
  var _this = this;
16693
16709
  if (options === void 0) { options = {
16694
16710
  mfaUrl: "/auth/multi-factor",
16711
+ storageKey: "CAMS-MSAL-AUTH-SDK",
16695
16712
  }; }
16696
16713
  var _a = useMsal(), instance = _a.instance, inProgress = _a.inProgress, accounts = _a.accounts;
16697
16714
  var account = useAccount(accounts[0] || {});
16698
- var storageKey = "CAMS-MSAL-AUTH-SDK";
16699
16715
  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];
16716
+ var _c = useState(""), idToken = _c[0], setIdToken = _c[1];
16717
+ var _d = useState(""), appCode = _d[0], setAppCode = _d[1];
16718
+ var _e = useState(""), accessToken = _e[0], setAccessToken = _e[1];
16704
16719
  var isLoading = inProgress !== InteractionStatus.None;
16705
- var isAuthenticated = !!account && !!token;
16720
+ var isAuthenticated = !!account && !!accessToken;
16706
16721
  var scopes = options.scopes || ["openid", "profile", "email"];
16707
16722
  var isTokenValid = function (token) {
16708
16723
  try {
@@ -16714,25 +16729,24 @@ function useCAMSMSALAuth(options) {
16714
16729
  }
16715
16730
  };
16716
16731
  useEffect(function () {
16717
- if (typeof window !== "undefined" && !token) {
16718
- var stored = localStorage.getItem(storageKey);
16732
+ if (typeof window !== "undefined" && !accessToken) {
16733
+ var stored = localStorage.getItem(options.storageKey);
16719
16734
  if (stored) {
16720
16735
  try {
16721
16736
  var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken, appCode_1 = _a.appCode;
16722
16737
  if (accessToken_1 && isTokenValid(accessToken_1)) {
16723
- setToken(accessToken_1);
16724
16738
  setAccessToken(accessToken_1);
16725
16739
  setIdToken(idToken_1);
16726
16740
  setAppCode(appCode_1);
16727
16741
  }
16728
16742
  else {
16729
- localStorage.removeItem(storageKey);
16743
+ localStorage.removeItem(options.storageKey);
16730
16744
  }
16731
16745
  }
16732
16746
  catch (_b) { }
16733
16747
  }
16734
16748
  }
16735
- }, [token]);
16749
+ }, [accessToken]);
16736
16750
  // useEffect(() => {
16737
16751
  // const handleRedirect = async () => {
16738
16752
  // try {
@@ -16786,12 +16800,11 @@ function useCAMSMSALAuth(options) {
16786
16800
  accessToken: accessToken,
16787
16801
  idToken: idToken,
16788
16802
  });
16789
- setToken(response.accessToken);
16790
16803
  setAccessToken(response.accessToken);
16791
16804
  setIdToken(response.idToken);
16792
16805
  // Persist tokens to localStorage
16793
16806
  if (typeof window !== "undefined") {
16794
- localStorage.setItem(storageKey, JSON.stringify({
16807
+ localStorage.setItem(options.storageKey, JSON.stringify({
16795
16808
  isAuthenticated: true,
16796
16809
  accessToken: response.accessToken,
16797
16810
  idToken: response.idToken,
@@ -16838,14 +16851,16 @@ function useCAMSMSALAuth(options) {
16838
16851
  var listener_1 = function (event) {
16839
16852
  if (event.source !== authWindow_1)
16840
16853
  return;
16841
- var allowedOrigins = options.allowedOrigins || [
16842
- options.messageOrigin || new URL(options.mfaUrl || "").origin
16843
- ].filter(Boolean);
16844
- if (allowedOrigins.length > 0 && !allowedOrigins.includes(event.origin))
16854
+ var allowedOrigins = options.allowedOrigins ||
16855
+ [
16856
+ options.messageOrigin || new URL(options.mfaUrl || "").origin,
16857
+ ].filter(Boolean);
16858
+ if (allowedOrigins.length > 0 &&
16859
+ !allowedOrigins.includes(event.origin))
16845
16860
  return;
16846
16861
  var tokenMsg = ProfileSchema.safeParse(event.data);
16847
16862
  if (tokenMsg.success) {
16848
- localStorage.setItem(storageKey, JSON.stringify({
16863
+ localStorage.setItem(options.storageKey, JSON.stringify({
16849
16864
  isAuthenticated: true,
16850
16865
  accessToken: response.accessToken,
16851
16866
  idToken: response.idToken,
@@ -16907,13 +16922,12 @@ function useCAMSMSALAuth(options) {
16907
16922
  return [4 /*yield*/, instance.logoutRedirect()];
16908
16923
  case 1:
16909
16924
  _a.sent();
16910
- setToken("");
16911
16925
  setAccessToken("");
16912
16926
  setIdToken("");
16913
16927
  setAppCode("");
16914
16928
  setError(null);
16915
16929
  if (typeof window !== "undefined") {
16916
- localStorage.removeItem(storageKey);
16930
+ localStorage.removeItem(options.storageKey);
16917
16931
  }
16918
16932
  return [3 /*break*/, 3];
16919
16933
  case 2:
@@ -16928,7 +16942,7 @@ function useCAMSMSALAuth(options) {
16928
16942
  return {
16929
16943
  login: login,
16930
16944
  logout: logout,
16931
- storageKey: storageKey,
16945
+ storageKey: options.storageKey,
16932
16946
  isAuthenticated: isAuthenticated,
16933
16947
  isLoading: isLoading,
16934
16948
  error: error,
@@ -17358,11 +17372,81 @@ if (process.env.NODE_ENV === 'production') {
17358
17372
  var jsxRuntimeExports = jsxRuntime.exports;
17359
17373
 
17360
17374
  var CAMSContext = createContext(null);
17375
+ var setCookie$1 = function (name, value, days) {
17376
+ var expires = new Date(Date.now() + days * 864e5).toUTCString();
17377
+ document.cookie = "".concat(name, "=").concat(encodeURIComponent(value), "; expires=").concat(expires, "; path=/; samesite=Lax");
17378
+ };
17379
+ var getCookie$1 = function (name) {
17380
+ var _a;
17381
+ return ((_a = document.cookie
17382
+ .split("; ")
17383
+ .find(function (row) { return row.startsWith(name + "="); })) === null || _a === void 0 ? void 0 : _a.split("=")[1])
17384
+ ? decodeURIComponent(document.cookie
17385
+ .split("; ")
17386
+ .find(function (row) { return row.startsWith(name + "="); })
17387
+ .split("=")[1])
17388
+ : null;
17389
+ };
17390
+ var deleteCookie$1 = function (name) {
17391
+ document.cookie = name + "=; Max-Age=-99999999; path=/";
17392
+ };
17361
17393
  function CAMSProvider(_a) {
17362
- var children = _a.children, defaultConfig = _a.defaultConfig, msalOptions = _a.msalOptions, authOptions = __rest(_a, ["children", "defaultConfig", "msalOptions"]);
17394
+ var _this = this;
17395
+ var children = _a.children, authOptions = __rest(_a, ["children"]);
17363
17396
  var auth = useCAMSAuth(authOptions);
17364
- var msalAuth = useCAMSMSALAuth(msalOptions || { mfaUrl: '/auth/multi-factor' });
17365
- 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]);
17397
+ var profileStorageKey = "".concat(auth.storageKey, "-PROFILE");
17398
+ var getInitialProfile = function () {
17399
+ if (typeof window === "undefined") {
17400
+ return null;
17401
+ }
17402
+ try {
17403
+ var storedProfile = getCookie$1(profileStorageKey);
17404
+ return storedProfile ? JSON.parse(storedProfile) : null;
17405
+ }
17406
+ catch (_a) {
17407
+ return null;
17408
+ }
17409
+ };
17410
+ var _b = useState(getInitialProfile), userProfile = _b[0], setUserProfile = _b[1];
17411
+ // Load profile from storage on mount
17412
+ useEffect(function () {
17413
+ if (typeof window !== "undefined") {
17414
+ var storedProfile = getCookie$1(profileStorageKey);
17415
+ if (storedProfile) {
17416
+ try {
17417
+ setUserProfile(JSON.parse(storedProfile));
17418
+ }
17419
+ catch (_a) { }
17420
+ }
17421
+ }
17422
+ }, [profileStorageKey]);
17423
+ // Persist profile separately
17424
+ useEffect(function () {
17425
+ if (typeof window !== "undefined") {
17426
+ if (userProfile) {
17427
+ setCookie$1(profileStorageKey, JSON.stringify(userProfile), 1);
17428
+ }
17429
+ else {
17430
+ deleteCookie$1(profileStorageKey);
17431
+ }
17432
+ }
17433
+ }, [userProfile, profileStorageKey]);
17434
+ // Enhanced logout that also clears profile
17435
+ var enhancedLogout = function () { return __awaiter(_this, void 0, void 0, function () {
17436
+ return __generator(this, function (_a) {
17437
+ switch (_a.label) {
17438
+ case 0: return [4 /*yield*/, auth.logout()];
17439
+ case 1:
17440
+ _a.sent();
17441
+ setUserProfile(null);
17442
+ if (typeof window !== "undefined") {
17443
+ deleteCookie$1(profileStorageKey);
17444
+ }
17445
+ return [2 /*return*/];
17446
+ }
17447
+ });
17448
+ }); };
17449
+ var value = useMemo(function () { return (__assign(__assign({}, auth), { logout: enhancedLogout, userProfile: userProfile, setUserProfile: setUserProfile })); }, [auth, userProfile]);
17366
17450
  return (jsxRuntimeExports.jsx(CAMSContext.Provider, { value: value, children: children }));
17367
17451
  }
17368
17452
  function useCAMSContext() {