@nibssplc/cams-sdk-react 0.0.1-beta.54 → 0.0.1-beta.57

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.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './hooks/useCAMSAuth';
2
2
  export * from './hooks/useCAMSMSALAuth';
3
+ export * from './hooks/useCAMSPopupAuth';
3
4
  export * from './components/ProtectedRoute';
4
5
  export { CAMSMSALProvider, useCAMSMSALContext } from './components/CAMSMSALProvider';
5
6
  export * from './components/ClientOnly';
package/dist/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as React__default from 'react';
2
2
  import React__default__default, { useState, useRef, useEffect, useCallback, useMemo, useReducer, useContext, createContext } from 'react';
3
- import { CAMSSessionManager, CAMSError, CAMSErrorType, Logger as Logger$1, ProfileSchema, AuthFailureMessageSchema, ErrorMessageSchema } from '@nibssplc/cams-sdk';
3
+ import { isPopupWindow, initializePopupAuth, CAMSSessionManager, CAMSError, CAMSErrorType, Logger as Logger$1, ProfileSchema, AuthFailureMessageSchema, ErrorMessageSchema } from '@nibssplc/cams-sdk';
4
4
  export * from '@nibssplc/cams-sdk';
5
5
 
6
6
  /******************************************************************************
@@ -103,6 +103,10 @@ function useCAMSAuth(options) {
103
103
  // Only initialize on client side
104
104
  if (typeof window === "undefined")
105
105
  return;
106
+ // Initialize popup auth handler if in popup
107
+ if (isPopupWindow()) {
108
+ initializePopupAuth();
109
+ }
106
110
  (_a = sessionManagerRef.current) !== null && _a !== void 0 ? _a : (sessionManagerRef.current = new CAMSSessionManager(localStorage, options.storageKey || "CAMS-SDK", {
107
111
  onAuthSuccess: function (res) {
108
112
  var _a;
@@ -128,12 +132,14 @@ function useCAMSAuth(options) {
128
132
  (_a = options.onTokenExpired) === null || _a === void 0 ? void 0 : _a.call(options);
129
133
  },
130
134
  }));
131
- // Check initial auth state
132
- var initialAuth = sessionManagerRef.current.isAuthenticated();
133
- setIsAuthenticated(initialAuth);
134
- if (initialAuth) {
135
- setToken((_b = sessionManagerRef.current.getAccessToken()) !== null && _b !== void 0 ? _b : "");
136
- sessionManagerRef.current.getProfile().then(setProfile);
135
+ // Check initial auth state (skip if in popup)
136
+ if (!isPopupWindow()) {
137
+ var initialAuth = sessionManagerRef.current.isAuthenticated();
138
+ setIsAuthenticated(initialAuth);
139
+ if (initialAuth) {
140
+ setToken((_b = sessionManagerRef.current.getAccessToken()) !== null && _b !== void 0 ? _b : "");
141
+ sessionManagerRef.current.getProfile().then(setProfile);
142
+ }
137
143
  }
138
144
  }, [options.storageKey]);
139
145
  var login = useCallback(function (config) { return __awaiter(_this, void 0, void 0, function () {
@@ -16707,6 +16713,7 @@ function useAccount(accountIdentifiers) {
16707
16713
  function useCAMSMSALAuth(options) {
16708
16714
  var _this = this;
16709
16715
  if (options === void 0) { options = {
16716
+ appCode: "",
16710
16717
  mfaUrl: "/auth/multi-factor",
16711
16718
  storageKey: "CAMS-MSAL-AUTH-SDK",
16712
16719
  }; }
@@ -16714,8 +16721,7 @@ function useCAMSMSALAuth(options) {
16714
16721
  var account = useAccount(accounts[0] || {});
16715
16722
  var _b = useState(null), error = _b[0], setError = _b[1];
16716
16723
  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];
16724
+ var _d = useState(""), accessToken = _d[0], setAccessToken = _d[1];
16719
16725
  var isLoading = inProgress !== InteractionStatus.None;
16720
16726
  var isAuthenticated = !!account && !!accessToken;
16721
16727
  var scopes = options.scopes || ["openid", "profile", "email"];
@@ -16733,11 +16739,11 @@ function useCAMSMSALAuth(options) {
16733
16739
  var stored = localStorage.getItem(options.storageKey);
16734
16740
  if (stored) {
16735
16741
  try {
16736
- var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken, appCode_1 = _a.appCode;
16742
+ var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken, appCode = _a.appCode;
16737
16743
  if (accessToken_1 && isTokenValid(accessToken_1)) {
16738
16744
  setAccessToken(accessToken_1);
16739
16745
  setIdToken(idToken_1);
16740
- setAppCode(appCode_1);
16746
+ console.log("App Code >>>", appCode, options.appCode);
16741
16747
  }
16742
16748
  else {
16743
16749
  localStorage.removeItem(options.storageKey);
@@ -16924,7 +16930,6 @@ function useCAMSMSALAuth(options) {
16924
16930
  _a.sent();
16925
16931
  setAccessToken("");
16926
16932
  setIdToken("");
16927
- setAppCode("");
16928
16933
  setError(null);
16929
16934
  if (typeof window !== "undefined") {
16930
16935
  localStorage.removeItem(options.storageKey);
@@ -16948,7 +16953,57 @@ function useCAMSMSALAuth(options) {
16948
16953
  error: error,
16949
16954
  idToken: idToken,
16950
16955
  accessToken: accessToken,
16951
- appCode: appCode,
16956
+ appCode: options.appCode,
16957
+ };
16958
+ }
16959
+
16960
+ /**
16961
+ * Hook for handling authentication in popup windows
16962
+ * This should be used by the popup app to complete authentication
16963
+ */
16964
+ function useCAMSPopupAuth(options) {
16965
+ if (options === void 0) { options = {}; }
16966
+ var _a = options.storageKey, storageKey = _a === void 0 ? "CAMS-SDK" : _a, targetOrigin = options.targetOrigin, onAuthComplete = options.onAuthComplete, onAuthError = options.onAuthError;
16967
+ useEffect(function () {
16968
+ if (typeof window === "undefined" || !isPopupWindow())
16969
+ return;
16970
+ // Initialize popup auth handler
16971
+ initializePopupAuth(targetOrigin);
16972
+ // Set up global handlers for the popup app
16973
+ var globalHandlers = window.__CAMS_POPUP_AUTH__;
16974
+ if (globalHandlers) {
16975
+ var originalCompleteAuth_1 = globalHandlers.completeAuth;
16976
+ var originalErrorAuth_1 = globalHandlers.errorAuth;
16977
+ globalHandlers.completeAuth = function (profile) {
16978
+ onAuthComplete === null || onAuthComplete === void 0 ? void 0 : onAuthComplete(profile);
16979
+ originalCompleteAuth_1(profile);
16980
+ };
16981
+ globalHandlers.errorAuth = function (error) {
16982
+ onAuthError === null || onAuthError === void 0 ? void 0 : onAuthError(error);
16983
+ originalErrorAuth_1(error);
16984
+ };
16985
+ }
16986
+ }, [targetOrigin, onAuthComplete, onAuthError]);
16987
+ var completeAuth = useCallback(function (profile) {
16988
+ if (!isPopupWindow()) {
16989
+ console.warn("completeAuth called outside of popup window");
16990
+ return;
16991
+ }
16992
+ var sessionManager = new CAMSSessionManager(localStorage, storageKey);
16993
+ sessionManager.completePopupAuth(profile, targetOrigin);
16994
+ }, [storageKey, targetOrigin]);
16995
+ var errorAuth = useCallback(function (error) {
16996
+ if (!isPopupWindow()) {
16997
+ console.warn("errorAuth called outside of popup window");
16998
+ return;
16999
+ }
17000
+ var sessionManager = new CAMSSessionManager(localStorage, storageKey);
17001
+ sessionManager.errorPopupAuth(error, targetOrigin);
17002
+ }, [storageKey, targetOrigin]);
17003
+ return {
17004
+ completeAuth: completeAuth,
17005
+ errorAuth: errorAuth,
17006
+ isPopup: isPopupWindow(),
16952
17007
  };
16953
17008
  }
16954
17009
 
@@ -19233,7 +19288,9 @@ function CAMSProviderInner(props) {
19233
19288
  var regularAuth = useCAMSAuth(mode === "REGULAR"
19234
19289
  ? __assign(__assign({}, props), { appCode: appCode })
19235
19290
  : { appCode: "" });
19236
- var msalAuth = useCAMSMSALAuth(mode === "MSAL" ? props : {});
19291
+ var msalAuth = useCAMSMSALAuth(mode === "MSAL"
19292
+ ? __assign(__assign({}, props), { appCode: appCode })
19293
+ : { appCode: "" });
19237
19294
  var auth = mode === "REGULAR" ? regularAuth : msalAuth;
19238
19295
  var profileStorageKey = "".concat(auth.storageKey, "-PROFILE");
19239
19296
  var getInitialProfile = function () {
@@ -19311,5 +19368,5 @@ function useCAMSContext() {
19311
19368
  // Backward compatibility exports
19312
19369
  var CAMSProvider = function (props) { return (jsxRuntimeExports.jsx(UnifiedCAMSProvider, __assign({}, props, { mode: "REGULAR" }))); };
19313
19370
 
19314
- export { CAMSMSALProvider, CAMSProvider, ClientOnly, ProtectedRoute, UnifiedCAMSProvider, useCAMSAuth, useCAMSContext, useCAMSMSALAuth, useCAMSMSALContext };
19371
+ export { CAMSMSALProvider, CAMSProvider, ClientOnly, ProtectedRoute, UnifiedCAMSProvider, useCAMSAuth, useCAMSContext, useCAMSMSALAuth, useCAMSMSALContext, useCAMSPopupAuth };
19315
19372
  //# sourceMappingURL=index.esm.js.map