@auth0/auth0-react 2.3.0 → 2.5.0

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.
@@ -28,7 +28,7 @@ import {
28
28
  deprecateRedirectUri,
29
29
  } from './utils';
30
30
  import { reducer } from './reducer';
31
- import { initialAuthState } from './auth-state';
31
+ import { initialAuthState, type AuthState } from './auth-state';
32
32
 
33
33
  /**
34
34
  * The state of the application before the user was redirected to the login page.
@@ -41,7 +41,7 @@ export type AppState = {
41
41
  /**
42
42
  * The main configuration to instantiate the `Auth0Provider`.
43
43
  */
44
- export interface Auth0ProviderOptions extends Auth0ClientOptions {
44
+ export interface Auth0ProviderOptions<TUser extends User = User> extends Auth0ClientOptions {
45
45
  /**
46
46
  * The child nodes your Provider has wrapped
47
47
  */
@@ -51,7 +51,7 @@ export interface Auth0ProviderOptions extends Auth0ClientOptions {
51
51
  * It uses `window.history` but you might want to overwrite this if you are using a custom router, like `react-router-dom`
52
52
  * See the EXAMPLES.md for more info.
53
53
  */
54
- onRedirectCallback?: (appState?: AppState, user?: User) => void;
54
+ onRedirectCallback?: (appState?: AppState, user?: TUser) => void;
55
55
  /**
56
56
  * By default, if the page url has code/state params, the SDK will treat them as Auth0's and attempt to exchange the
57
57
  * code for a token. In some cases the code might be for something else (another OAuth SDK perhaps). In these
@@ -83,7 +83,7 @@ export interface Auth0ProviderOptions extends Auth0ClientOptions {
83
83
  *
84
84
  * For a sample on using multiple Auth0Providers review the [React Account Linking Sample](https://github.com/auth0-samples/auth0-link-accounts-sample/tree/react-variant)
85
85
  */
86
- context?: React.Context<Auth0ContextInterface>;
86
+ context?: React.Context<Auth0ContextInterface<TUser>>;
87
87
  }
88
88
 
89
89
  /**
@@ -116,7 +116,7 @@ const defaultOnRedirectCallback = (appState?: AppState): void => {
116
116
  window.history.replaceState(
117
117
  {},
118
118
  document.title,
119
- appState?.returnTo || window.location.pathname
119
+ appState?.returnTo ?? window.location.pathname
120
120
  );
121
121
  };
122
122
 
@@ -132,7 +132,7 @@ const defaultOnRedirectCallback = (appState?: AppState): void => {
132
132
  *
133
133
  * Provides the Auth0Context to its child components.
134
134
  */
135
- const Auth0Provider = (opts: Auth0ProviderOptions) => {
135
+ const Auth0Provider = <TUser extends User = User>(opts: Auth0ProviderOptions<TUser>) => {
136
136
  const {
137
137
  children,
138
138
  skipRedirectCallback,
@@ -143,7 +143,7 @@ const Auth0Provider = (opts: Auth0ProviderOptions) => {
143
143
  const [client] = useState(
144
144
  () => new Auth0Client(toAuth0ClientOptions(clientOpts))
145
145
  );
146
- const [state, dispatch] = useReducer(reducer, initialAuthState);
146
+ const [state, dispatch] = useReducer(reducer<TUser>, initialAuthState as AuthState<TUser>);
147
147
  const didInitialise = useRef(false);
148
148
 
149
149
  const handleError = useCallback((error: Error) => {
@@ -158,7 +158,7 @@ const Auth0Provider = (opts: Auth0ProviderOptions) => {
158
158
  didInitialise.current = true;
159
159
  (async (): Promise<void> => {
160
160
  try {
161
- let user: User | undefined;
161
+ let user: TUser | undefined;
162
162
  if (hasAuthParams() && !skipRedirectCallback) {
163
163
  const { appState } = await client.handleRedirectCallback();
164
164
  user = await client.getUser();
@@ -198,7 +198,7 @@ const Auth0Provider = (opts: Auth0ProviderOptions) => {
198
198
  const user = await client.getUser();
199
199
  dispatch({ type: 'LOGIN_POPUP_COMPLETE', user });
200
200
  },
201
- [client]
201
+ [client, handleError]
202
202
  );
203
203
 
204
204
  const logout = useCallback(
@@ -272,7 +272,27 @@ const Auth0Provider = (opts: Auth0ProviderOptions) => {
272
272
  [client]
273
273
  );
274
274
 
275
- const contextValue = useMemo<Auth0ContextInterface<User>>(() => {
275
+ const getDpopNonce = useCallback<Auth0Client['getDpopNonce']>(
276
+ (id) => client.getDpopNonce(id),
277
+ [client]
278
+ );
279
+
280
+ const setDpopNonce = useCallback<Auth0Client['setDpopNonce']>(
281
+ (nonce, id) => client.setDpopNonce(nonce, id),
282
+ [client]
283
+ );
284
+
285
+ const generateDpopProof = useCallback<Auth0Client['generateDpopProof']>(
286
+ (params) => client.generateDpopProof(params),
287
+ [client]
288
+ );
289
+
290
+ const createFetcher = useCallback<Auth0Client['createFetcher']>(
291
+ (config) => client.createFetcher(config),
292
+ [client]
293
+ );
294
+
295
+ const contextValue = useMemo<Auth0ContextInterface<TUser>>(() => {
276
296
  return {
277
297
  ...state,
278
298
  getAccessTokenSilently,
@@ -282,6 +302,10 @@ const Auth0Provider = (opts: Auth0ProviderOptions) => {
282
302
  loginWithPopup,
283
303
  logout,
284
304
  handleRedirectCallback,
305
+ getDpopNonce,
306
+ setDpopNonce,
307
+ generateDpopProof,
308
+ createFetcher,
285
309
  };
286
310
  }, [
287
311
  state,
@@ -292,6 +316,10 @@ const Auth0Provider = (opts: Auth0ProviderOptions) => {
292
316
  loginWithPopup,
293
317
  logout,
294
318
  handleRedirectCallback,
319
+ getDpopNonce,
320
+ setDpopNonce,
321
+ generateDpopProof,
322
+ createFetcher,
295
323
  ]);
296
324
 
297
325
  return <context.Provider value={contextValue}>{children}</context.Provider>;
package/src/errors.tsx CHANGED
@@ -6,7 +6,7 @@
6
6
  */
7
7
  export class OAuthError extends Error {
8
8
  constructor(public error: string, public error_description?: string) {
9
- super(error_description || error);
9
+ super(error_description ?? error);
10
10
 
11
11
  // https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
12
12
  Object.setPrototypeOf(this, OAuthError.prototype);
package/src/index.tsx CHANGED
@@ -36,6 +36,8 @@ export {
36
36
  PopupTimeoutError,
37
37
  AuthenticationError,
38
38
  MissingRefreshTokenError,
39
- GenericError
39
+ GenericError,
40
+ UseDpopNonceError,
41
+ type FetcherConfig
40
42
  } from '@auth0/auth0-spa-js';
41
43
  export { OAuthError } from './errors';
package/src/reducer.tsx CHANGED
@@ -9,7 +9,7 @@ type Action =
9
9
  | 'LOGIN_POPUP_COMPLETE'
10
10
  | 'GET_ACCESS_TOKEN_COMPLETE'
11
11
  | 'HANDLE_REDIRECT_COMPLETE';
12
- user?: User;
12
+ user: User | undefined;
13
13
  }
14
14
  | { type: 'LOGOUT' }
15
15
  | { type: 'ERROR'; error: Error };
@@ -17,7 +17,7 @@ type Action =
17
17
  /**
18
18
  * Handles how that state changes in the `useAuth0` hook.
19
19
  */
20
- export const reducer = (state: AuthState, action: Action): AuthState => {
20
+ export const reducer = <TUser extends User = User>(state: AuthState<TUser>, action: Action): AuthState<TUser> => {
21
21
  switch (action.type) {
22
22
  case 'LOGIN_POPUP_STARTED':
23
23
  return {
@@ -29,7 +29,7 @@ export const reducer = (state: AuthState, action: Action): AuthState => {
29
29
  return {
30
30
  ...state,
31
31
  isAuthenticated: !!action.user,
32
- user: action.user,
32
+ user: action.user as TUser | undefined,
33
33
  isLoading: false,
34
34
  error: undefined,
35
35
  };
@@ -41,7 +41,7 @@ export const reducer = (state: AuthState, action: Action): AuthState => {
41
41
  return {
42
42
  ...state,
43
43
  isAuthenticated: !!action.user,
44
- user: action.user,
44
+ user: action.user as TUser | undefined,
45
45
  };
46
46
  case 'LOGOUT':
47
47
  return {
package/src/utils.tsx CHANGED
@@ -47,7 +47,7 @@ export const deprecateRedirectUri = (options?: any) => {
47
47
  console.warn(
48
48
  'Using `redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `redirectUri` will be no longer supported in a future version'
49
49
  );
50
- options.authorizationParams = options.authorizationParams || {};
50
+ options.authorizationParams = options.authorizationParams ?? {};
51
51
  options.authorizationParams.redirect_uri = options.redirectUri;
52
52
  delete options.redirectUri;
53
53
  }
@@ -117,11 +117,11 @@ const withAuthenticationRequired = <P extends object>(
117
117
  const opts = {
118
118
  ...loginOptions,
119
119
  appState: {
120
- ...(loginOptions && loginOptions.appState),
120
+ ...loginOptions?.appState,
121
121
  returnTo: typeof returnTo === 'function' ? returnTo() : returnTo,
122
122
  },
123
123
  };
124
- (async (): Promise<void> => {
124
+ void (async (): Promise<void> => {
125
125
  await onBeforeAuthentication();
126
126
  await loginWithRedirect(opts);
127
127
  })();