@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/README.md +69 -148
- package/dist/auth0-context.d.ts +27 -43
- package/dist/auth0-context.d.ts.map +1 -1
- package/dist/auth0-provider.d.ts +16 -100
- package/dist/auth0-provider.d.ts.map +1 -1
- package/dist/auth0-react.cjs.js +46 -64
- package/dist/auth0-react.cjs.js.map +1 -1
- package/dist/auth0-react.esm.js +42 -55
- package/dist/auth0-react.esm.js.map +1 -1
- package/dist/auth0-react.js +46 -64
- package/dist/auth0-react.js.map +1 -1
- package/dist/auth0-react.min.js +1 -15
- package/dist/auth0-react.min.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/reducer.d.ts +1 -1
- package/dist/reducer.d.ts.map +1 -1
- package/dist/use-auth0.d.ts +2 -1
- package/dist/use-auth0.d.ts.map +1 -1
- package/dist/with-auth0.d.ts +5 -2
- package/dist/with-auth0.d.ts.map +1 -1
- package/dist/with-authentication-required.d.ts +5 -4
- package/dist/with-authentication-required.d.ts.map +1 -1
- package/package.json +33 -32
- package/src/auth0-context.tsx +18 -58
- package/src/auth0-provider.tsx +27 -155
- package/src/index.tsx +3 -2
- package/src/reducer.tsx +1 -1
- package/src/use-auth0.tsx +5 -3
- package/src/with-auth0.tsx +8 -4
- package/src/with-authentication-required.tsx +14 -14
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>(
|
|
28
|
-
|
|
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;
|
package/src/with-auth0.tsx
CHANGED
|
@@ -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
|
-
<
|
|
35
|
+
<context.Consumer>
|
|
32
36
|
{(auth: Auth0ContextInterface): JSX.Element => (
|
|
33
37
|
<Component {...(props as P)} auth0={auth} />
|
|
34
38
|
)}
|
|
35
|
-
</
|
|
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
|
-
*
|
|
65
|
-
*
|
|
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
|
-
|
|
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
|
-
|
|
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 ||
|
|
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
|
-
|
|
113
|
+
isAuthenticated,
|
|
114
114
|
loginWithRedirect,
|
|
115
115
|
loginOptions,
|
|
116
116
|
returnTo,
|
|
117
117
|
]);
|
|
118
118
|
|
|
119
|
-
return
|
|
119
|
+
return isAuthenticated ? <Component {...props} /> : onRedirecting();
|
|
120
120
|
};
|
|
121
121
|
};
|
|
122
122
|
|