@auth0/auth0-react 1.11.0 → 2.0.0-beta.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.
package/src/use-auth0.tsx CHANGED
@@ -24,7 +24,9 @@ import Auth0Context, { Auth0ContextInterface } from './auth0-context';
24
24
  *
25
25
  * TUser is an optional type param to provide a type to the `user` field.
26
26
  */
27
- const useAuth0 = <TUser extends User = User>(): Auth0ContextInterface<TUser> =>
28
- useContext(Auth0Context) as Auth0ContextInterface<TUser>;
27
+ const useAuth0 = <TUser extends User = User>(
28
+ context = Auth0Context
29
+ ): Auth0ContextInterface<TUser> =>
30
+ useContext(context) as Auth0ContextInterface<TUser>;
29
31
 
30
- export default useAuth0;
32
+ export default useAuth0;
@@ -21,18 +21,22 @@ export interface WithAuth0Props {
21
21
  * export default withAuth0(MyComponent);
22
22
  * ```
23
23
  *
24
- * Wrap your class components in this Higher Order Component to give them access to the Auth0Context
24
+ * Wrap your class components in this Higher Order Component to give them access to the Auth0Context.
25
+ *
26
+ * Providing a context as the second argument allows you to configure the Auth0Provider the Auth0Context
27
+ * should come from f you have multiple within your application.
25
28
  */
26
29
  const withAuth0 = <P extends WithAuth0Props>(
27
- Component: ComponentType<P>
30
+ Component: ComponentType<P>,
31
+ context = Auth0Context
28
32
  ): ComponentType<Omit<P, keyof WithAuth0Props>> => {
29
33
  return function WithAuth(props): JSX.Element {
30
34
  return (
31
- <Auth0Context.Consumer>
35
+ <context.Consumer>
32
36
  {(auth: Auth0ContextInterface): JSX.Element => (
33
37
  <Component {...(props as P)} auth0={auth} />
34
38
  )}
35
- </Auth0Context.Consumer>
39
+ </context.Consumer>
36
40
  );
37
41
  };
38
42
  };
@@ -1,6 +1,9 @@
1
1
  import React, { ComponentType, useEffect, FC } from 'react';
2
- import { RedirectLoginOptions, User } from '@auth0/auth0-spa-js';
3
2
  import useAuth0 from './use-auth0';
3
+ import Auth0Context, {
4
+ Auth0ContextInterface,
5
+ RedirectLoginOptions,
6
+ } from './auth0-context';
4
7
 
5
8
  /**
6
9
  * @ignore
@@ -61,10 +64,11 @@ export interface WithAuthenticationRequiredOptions {
61
64
  */
62
65
  loginOptions?: RedirectLoginOptions;
63
66
  /**
64
- * Check the user object for JWT claims and return a boolean indicating
65
- * whether or not they are authorized to view the component.
67
+ * The context to be used when calling useAuth0, this should only be provided if you are using multiple Auth0Providers
68
+ * within your application and you wish to tie a specific component to a Auth0Provider other than the Auth0Provider
69
+ * associated with the default Auth0Context.
66
70
  */
67
- claimCheck?: (claims?: User) => boolean;
71
+ context?: React.Context<Auth0ContextInterface>;
68
72
  }
69
73
 
70
74
  /**
@@ -80,22 +84,18 @@ const withAuthenticationRequired = <P extends object>(
80
84
  options: WithAuthenticationRequiredOptions = {}
81
85
  ): FC<P> => {
82
86
  return function WithAuthenticationRequired(props: P): JSX.Element {
83
- const { user, isAuthenticated, isLoading, loginWithRedirect } = useAuth0();
84
87
  const {
85
88
  returnTo = defaultReturnTo,
86
89
  onRedirecting = defaultOnRedirecting,
87
- claimCheck = (): boolean => true,
88
90
  loginOptions,
91
+ context = Auth0Context,
89
92
  } = options;
90
93
 
91
- /**
92
- * The route is authenticated if the user has valid auth and there are no
93
- * JWT claim mismatches.
94
- */
95
- const routeIsAuthenticated = isAuthenticated && claimCheck(user);
94
+ const { isAuthenticated, isLoading, loginWithRedirect } =
95
+ useAuth0(context);
96
96
 
97
97
  useEffect(() => {
98
- if (isLoading || routeIsAuthenticated) {
98
+ if (isLoading || isAuthenticated) {
99
99
  return;
100
100
  }
101
101
  const opts = {
@@ -110,13 +110,13 @@ const withAuthenticationRequired = <P extends object>(
110
110
  })();
111
111
  }, [
112
112
  isLoading,
113
- routeIsAuthenticated,
113
+ isAuthenticated,
114
114
  loginWithRedirect,
115
115
  loginOptions,
116
116
  returnTo,
117
117
  ]);
118
118
 
119
- return routeIsAuthenticated ? <Component {...props} /> : onRedirecting();
119
+ return isAuthenticated ? <Component {...props} /> : onRedirecting();
120
120
  };
121
121
  };
122
122