@nibssplc/cams-sdk-react 0.0.1-beta.30 → 0.0.1-beta.32

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.
@@ -1,8 +1,16 @@
1
1
  import React from 'react';
2
2
  import { PublicClientApplication, Configuration } from '@azure/msal-browser';
3
- export interface CAMSMSALProviderProps {
3
+ import { UseCAMSMSALAuthReturn, UseCAMSMSALAuthOptions } from '../hooks/useCAMSMSALAuth';
4
+ interface CAMSMSALContextValue extends UseCAMSMSALAuthReturn {
5
+ isAuthenticated: boolean;
6
+ idToken: string | null;
7
+ accessToken: string | null;
8
+ }
9
+ export interface CAMSMSALProviderProps extends UseCAMSMSALAuthOptions {
4
10
  children: React.ReactNode;
5
11
  msalConfig: Configuration;
6
12
  msalInstance?: PublicClientApplication;
7
13
  }
8
- export declare function CAMSMSALProvider({ children, msalConfig, msalInstance }: CAMSMSALProviderProps): import("react/jsx-runtime").JSX.Element;
14
+ export declare function CAMSMSALProvider(props: CAMSMSALProviderProps): import("react/jsx-runtime").JSX.Element;
15
+ export declare function useCAMSMSALContext(): CAMSMSALContextValue;
16
+ export {};
@@ -4,7 +4,7 @@ export interface UseCAMSMSALAuthOptions {
4
4
  onAuthError?: (error: CAMSError) => void;
5
5
  onTokenExpired?: () => void;
6
6
  scopes?: string[];
7
- mfaUrl: string;
7
+ mfaUrl?: string;
8
8
  prompt?: string;
9
9
  }
10
10
  export interface UseCAMSMSALAuthReturn {
package/dist/index.cjs.js CHANGED
@@ -16719,11 +16719,12 @@ function useCAMSMSALAuth(options) {
16719
16719
  var _d = React__default.useState(null), idToken = _d[0], setIdToken = _d[1];
16720
16720
  var _e = React__default.useState(null), accessToken = _e[0], setAccessToken = _e[1];
16721
16721
  var _f = React__default.useState(false), isMFAPending = _f[0], setIsMFAPending = _f[1];
16722
+ var _g = React__default.useState(false), hasRedirected = _g[0], setHasRedirected = _g[1];
16722
16723
  var isLoading = inProgress !== InteractionStatus.None;
16723
16724
  var isAuthenticated = !!account && !!token && !isMFAPending;
16724
16725
  var scopes = options.scopes || ["openid", "profile", "email"];
16725
16726
  React__default.useEffect(function () {
16726
- if (account && !isMFAPending) {
16727
+ if (account && !isMFAPending && !hasRedirected) {
16727
16728
  // Get token in foreground popup
16728
16729
  instance
16729
16730
  .acquireTokenPopup({
@@ -16737,9 +16738,13 @@ function useCAMSMSALAuth(options) {
16737
16738
  setAccessToken(response.accessToken);
16738
16739
  setIdToken(response.idToken);
16739
16740
  setIsMFAPending(true);
16740
- if (typeof window !== "undefined") {
16741
- window.location.href = options.mfaUrl;
16742
- }
16741
+ setHasRedirected(true);
16742
+ // Small delay to ensure popup closes before redirect
16743
+ setTimeout(function () {
16744
+ if (typeof window !== "undefined" && process.env.NODE_ENV !== 'test') {
16745
+ window.location.href = options.mfaUrl;
16746
+ }
16747
+ }, 100);
16743
16748
  (_a = options.onAuthSuccess) === null || _a === void 0 ? void 0 : _a.call(options, response.accessToken);
16744
16749
  })
16745
16750
  .catch(function (err) {
@@ -16749,7 +16754,7 @@ function useCAMSMSALAuth(options) {
16749
16754
  (_a = options.onAuthError) === null || _a === void 0 ? void 0 : _a.call(options, camsError);
16750
16755
  });
16751
16756
  }
16752
- }, [account, instance, isMFAPending]);
16757
+ }, [account, instance, isMFAPending, hasRedirected]);
16753
16758
  var login = React__default.useCallback(function () { return __awaiter(_this, void 0, void 0, function () {
16754
16759
  var err_1, redirectErr_1, camsError_1, camsError;
16755
16760
  var _a, _b, _c;
@@ -16760,7 +16765,7 @@ function useCAMSMSALAuth(options) {
16760
16765
  _d.label = 1;
16761
16766
  case 1:
16762
16767
  _d.trys.push([1, 3, , 8]);
16763
- return [4 /*yield*/, instance.loginPopup({
16768
+ return [4 /*yield*/, instance.loginRedirect({
16764
16769
  scopes: scopes,
16765
16770
  prompt: options.prompt || "login",
16766
16771
  })];
@@ -16808,6 +16813,7 @@ function useCAMSMSALAuth(options) {
16808
16813
  setToken(null);
16809
16814
  setError(null);
16810
16815
  setIsMFAPending(false);
16816
+ setHasRedirected(false);
16811
16817
  return [3 /*break*/, 3];
16812
16818
  case 2:
16813
16819
  err_2 = _a.sent();
@@ -17253,16 +17259,9 @@ var jsxRuntimeExports = jsxRuntime.exports;
17253
17259
  var CAMSContext = React__default.createContext(null);
17254
17260
  function CAMSProvider(_a) {
17255
17261
  var children = _a.children, defaultConfig = _a.defaultConfig, msalOptions = _a.msalOptions, authOptions = __rest(_a, ["children", "defaultConfig", "msalOptions"]);
17256
- var _b = React__default.useState(false), mounted = _b[0], setMounted = _b[1];
17257
17262
  var auth = useCAMSAuth(authOptions);
17258
17263
  var msalAuth = useCAMSMSALAuth(msalOptions || { mfaUrl: '/auth/multi-factor' });
17259
- React__default.useEffect(function () {
17260
- setMounted(true);
17261
- }, []);
17262
17264
  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]);
17263
- if (!mounted) {
17264
- return jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: children });
17265
- }
17266
17265
  return (jsxRuntimeExports.jsx(CAMSContext.Provider, { value: value, children: children }));
17267
17266
  }
17268
17267
  function useCAMSContext() {
@@ -17289,10 +17288,48 @@ function ProtectedRoute(_a) {
17289
17288
  return jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: children });
17290
17289
  }
17291
17290
 
17292
- function CAMSMSALProvider(_a) {
17293
- var children = _a.children, msalConfig = _a.msalConfig, msalInstance = _a.msalInstance;
17291
+ var CAMSMSALContext = React__default.createContext(null);
17292
+ function CAMSMSALProviderInner(_a) {
17293
+ var children = _a.children, authOptions = __rest(_a, ["children"]);
17294
+ var auth = useCAMSMSALAuth(authOptions);
17295
+ var _b = React__default.useState({
17296
+ isAuthenticated: false,
17297
+ idToken: null,
17298
+ accessToken: null
17299
+ }), persistedAuth = _b[0], setPersistedAuth = _b[1];
17300
+ React__default.useEffect(function () {
17301
+ if (typeof window !== 'undefined') {
17302
+ var stored = localStorage.getItem('cams-msal-auth');
17303
+ if (stored) {
17304
+ setPersistedAuth(JSON.parse(stored));
17305
+ }
17306
+ }
17307
+ }, []);
17308
+ React__default.useEffect(function () {
17309
+ var authState = {
17310
+ isAuthenticated: auth.isAuthenticated,
17311
+ idToken: auth.idToken,
17312
+ accessToken: auth.accessToken
17313
+ };
17314
+ setPersistedAuth(authState);
17315
+ if (typeof window !== 'undefined') {
17316
+ localStorage.setItem('cams-msal-auth', JSON.stringify(authState));
17317
+ }
17318
+ }, [auth.isAuthenticated, auth.idToken, auth.accessToken]);
17319
+ var value = React__default.useMemo(function () { return (__assign(__assign({}, auth), { isAuthenticated: persistedAuth.isAuthenticated, idToken: persistedAuth.idToken, accessToken: persistedAuth.accessToken })); }, [auth, persistedAuth]);
17320
+ return (jsxRuntimeExports.jsx(CAMSMSALContext.Provider, { value: value, children: children }));
17321
+ }
17322
+ function CAMSMSALProvider(props) {
17323
+ var msalConfig = props.msalConfig, msalInstance = props.msalInstance;
17294
17324
  var instance = msalInstance || new PublicClientApplication(msalConfig);
17295
- return (jsxRuntimeExports.jsx(MsalProvider, { instance: instance, children: children }));
17325
+ return (jsxRuntimeExports.jsx(MsalProvider, { instance: instance, children: jsxRuntimeExports.jsx(CAMSMSALProviderInner, __assign({}, props)) }));
17326
+ }
17327
+ function useCAMSMSALContext() {
17328
+ var context = React__default.useContext(CAMSMSALContext);
17329
+ if (!context) {
17330
+ throw new Error('useCAMSMSALContext must be used within a CAMSMSALProvider');
17331
+ }
17332
+ return context;
17296
17333
  }
17297
17334
 
17298
17335
  function ClientOnly(_a) {
@@ -17314,6 +17351,7 @@ exports.ProtectedRoute = ProtectedRoute;
17314
17351
  exports.useCAMSAuth = useCAMSAuth;
17315
17352
  exports.useCAMSContext = useCAMSContext;
17316
17353
  exports.useCAMSMSALAuth = useCAMSMSALAuth;
17354
+ exports.useCAMSMSALContext = useCAMSMSALContext;
17317
17355
  Object.keys(camsSdk).forEach(function (k) {
17318
17356
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
17319
17357
  enumerable: true,