@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.
@@ -8,6 +8,7 @@ export interface UseCAMSMSALAuthOptions {
8
8
  mfaUrl?: string;
9
9
  prompt?: string;
10
10
  messageOrigin?: string;
11
+ appCode: string;
11
12
  allowedOrigins?: string[];
12
13
  }
13
14
  export interface UseCAMSMSALAuthReturn {
@@ -0,0 +1,17 @@
1
+ import { Profile } from "@nibssplc/cams-sdk";
2
+ export interface UseCAMSPopupAuthOptions {
3
+ storageKey?: string;
4
+ targetOrigin?: string;
5
+ onAuthComplete?: (profile: Profile) => void;
6
+ onAuthError?: (error: any) => void;
7
+ }
8
+ export interface UseCAMSPopupAuthReturn {
9
+ completeAuth: (profile: Profile) => void;
10
+ errorAuth: (error: any) => void;
11
+ isPopup: boolean;
12
+ }
13
+ /**
14
+ * Hook for handling authentication in popup windows
15
+ * This should be used by the popup app to complete authentication
16
+ */
17
+ export declare function useCAMSPopupAuth(options?: UseCAMSPopupAuthOptions): UseCAMSPopupAuthReturn;
package/dist/index.cjs.js CHANGED
@@ -122,6 +122,10 @@ function useCAMSAuth(options) {
122
122
  // Only initialize on client side
123
123
  if (typeof window === "undefined")
124
124
  return;
125
+ // Initialize popup auth handler if in popup
126
+ if (camsSdk.isPopupWindow()) {
127
+ camsSdk.initializePopupAuth();
128
+ }
125
129
  (_a = sessionManagerRef.current) !== null && _a !== void 0 ? _a : (sessionManagerRef.current = new camsSdk.CAMSSessionManager(localStorage, options.storageKey || "CAMS-SDK", {
126
130
  onAuthSuccess: function (res) {
127
131
  var _a;
@@ -147,12 +151,14 @@ function useCAMSAuth(options) {
147
151
  (_a = options.onTokenExpired) === null || _a === void 0 ? void 0 : _a.call(options);
148
152
  },
149
153
  }));
150
- // Check initial auth state
151
- var initialAuth = sessionManagerRef.current.isAuthenticated();
152
- setIsAuthenticated(initialAuth);
153
- if (initialAuth) {
154
- setToken((_b = sessionManagerRef.current.getAccessToken()) !== null && _b !== void 0 ? _b : "");
155
- sessionManagerRef.current.getProfile().then(setProfile);
154
+ // Check initial auth state (skip if in popup)
155
+ if (!camsSdk.isPopupWindow()) {
156
+ var initialAuth = sessionManagerRef.current.isAuthenticated();
157
+ setIsAuthenticated(initialAuth);
158
+ if (initialAuth) {
159
+ setToken((_b = sessionManagerRef.current.getAccessToken()) !== null && _b !== void 0 ? _b : "");
160
+ sessionManagerRef.current.getProfile().then(setProfile);
161
+ }
156
162
  }
157
163
  }, [options.storageKey]);
158
164
  var login = React__default.useCallback(function (config) { return __awaiter(_this, void 0, void 0, function () {
@@ -16726,6 +16732,7 @@ function useAccount(accountIdentifiers) {
16726
16732
  function useCAMSMSALAuth(options) {
16727
16733
  var _this = this;
16728
16734
  if (options === void 0) { options = {
16735
+ appCode: "",
16729
16736
  mfaUrl: "/auth/multi-factor",
16730
16737
  storageKey: "CAMS-MSAL-AUTH-SDK",
16731
16738
  }; }
@@ -16733,8 +16740,7 @@ function useCAMSMSALAuth(options) {
16733
16740
  var account = useAccount(accounts[0] || {});
16734
16741
  var _b = React__default.useState(null), error = _b[0], setError = _b[1];
16735
16742
  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];
16743
+ var _d = React__default.useState(""), accessToken = _d[0], setAccessToken = _d[1];
16738
16744
  var isLoading = inProgress !== InteractionStatus.None;
16739
16745
  var isAuthenticated = !!account && !!accessToken;
16740
16746
  var scopes = options.scopes || ["openid", "profile", "email"];
@@ -16752,11 +16758,11 @@ function useCAMSMSALAuth(options) {
16752
16758
  var stored = localStorage.getItem(options.storageKey);
16753
16759
  if (stored) {
16754
16760
  try {
16755
- var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken, appCode_1 = _a.appCode;
16761
+ var _a = JSON.parse(stored), accessToken_1 = _a.accessToken, idToken_1 = _a.idToken, appCode = _a.appCode;
16756
16762
  if (accessToken_1 && isTokenValid(accessToken_1)) {
16757
16763
  setAccessToken(accessToken_1);
16758
16764
  setIdToken(idToken_1);
16759
- setAppCode(appCode_1);
16765
+ console.log("App Code >>>", appCode, options.appCode);
16760
16766
  }
16761
16767
  else {
16762
16768
  localStorage.removeItem(options.storageKey);
@@ -16943,7 +16949,6 @@ function useCAMSMSALAuth(options) {
16943
16949
  _a.sent();
16944
16950
  setAccessToken("");
16945
16951
  setIdToken("");
16946
- setAppCode("");
16947
16952
  setError(null);
16948
16953
  if (typeof window !== "undefined") {
16949
16954
  localStorage.removeItem(options.storageKey);
@@ -16967,7 +16972,57 @@ function useCAMSMSALAuth(options) {
16967
16972
  error: error,
16968
16973
  idToken: idToken,
16969
16974
  accessToken: accessToken,
16970
- appCode: appCode,
16975
+ appCode: options.appCode,
16976
+ };
16977
+ }
16978
+
16979
+ /**
16980
+ * Hook for handling authentication in popup windows
16981
+ * This should be used by the popup app to complete authentication
16982
+ */
16983
+ function useCAMSPopupAuth(options) {
16984
+ if (options === void 0) { options = {}; }
16985
+ var _a = options.storageKey, storageKey = _a === void 0 ? "CAMS-SDK" : _a, targetOrigin = options.targetOrigin, onAuthComplete = options.onAuthComplete, onAuthError = options.onAuthError;
16986
+ React__default.useEffect(function () {
16987
+ if (typeof window === "undefined" || !camsSdk.isPopupWindow())
16988
+ return;
16989
+ // Initialize popup auth handler
16990
+ camsSdk.initializePopupAuth(targetOrigin);
16991
+ // Set up global handlers for the popup app
16992
+ var globalHandlers = window.__CAMS_POPUP_AUTH__;
16993
+ if (globalHandlers) {
16994
+ var originalCompleteAuth_1 = globalHandlers.completeAuth;
16995
+ var originalErrorAuth_1 = globalHandlers.errorAuth;
16996
+ globalHandlers.completeAuth = function (profile) {
16997
+ onAuthComplete === null || onAuthComplete === void 0 ? void 0 : onAuthComplete(profile);
16998
+ originalCompleteAuth_1(profile);
16999
+ };
17000
+ globalHandlers.errorAuth = function (error) {
17001
+ onAuthError === null || onAuthError === void 0 ? void 0 : onAuthError(error);
17002
+ originalErrorAuth_1(error);
17003
+ };
17004
+ }
17005
+ }, [targetOrigin, onAuthComplete, onAuthError]);
17006
+ var completeAuth = React__default.useCallback(function (profile) {
17007
+ if (!camsSdk.isPopupWindow()) {
17008
+ console.warn("completeAuth called outside of popup window");
17009
+ return;
17010
+ }
17011
+ var sessionManager = new camsSdk.CAMSSessionManager(localStorage, storageKey);
17012
+ sessionManager.completePopupAuth(profile, targetOrigin);
17013
+ }, [storageKey, targetOrigin]);
17014
+ var errorAuth = React__default.useCallback(function (error) {
17015
+ if (!camsSdk.isPopupWindow()) {
17016
+ console.warn("errorAuth called outside of popup window");
17017
+ return;
17018
+ }
17019
+ var sessionManager = new camsSdk.CAMSSessionManager(localStorage, storageKey);
17020
+ sessionManager.errorPopupAuth(error, targetOrigin);
17021
+ }, [storageKey, targetOrigin]);
17022
+ return {
17023
+ completeAuth: completeAuth,
17024
+ errorAuth: errorAuth,
17025
+ isPopup: camsSdk.isPopupWindow(),
16971
17026
  };
16972
17027
  }
16973
17028
 
@@ -19252,7 +19307,9 @@ function CAMSProviderInner(props) {
19252
19307
  var regularAuth = useCAMSAuth(mode === "REGULAR"
19253
19308
  ? __assign(__assign({}, props), { appCode: appCode })
19254
19309
  : { appCode: "" });
19255
- var msalAuth = useCAMSMSALAuth(mode === "MSAL" ? props : {});
19310
+ var msalAuth = useCAMSMSALAuth(mode === "MSAL"
19311
+ ? __assign(__assign({}, props), { appCode: appCode })
19312
+ : { appCode: "" });
19256
19313
  var auth = mode === "REGULAR" ? regularAuth : msalAuth;
19257
19314
  var profileStorageKey = "".concat(auth.storageKey, "-PROFILE");
19258
19315
  var getInitialProfile = function () {
@@ -19339,6 +19396,7 @@ exports.useCAMSAuth = useCAMSAuth;
19339
19396
  exports.useCAMSContext = useCAMSContext;
19340
19397
  exports.useCAMSMSALAuth = useCAMSMSALAuth;
19341
19398
  exports.useCAMSMSALContext = useCAMSMSALContext;
19399
+ exports.useCAMSPopupAuth = useCAMSPopupAuth;
19342
19400
  Object.keys(camsSdk).forEach(function (k) {
19343
19401
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
19344
19402
  enumerable: true,