@versini/auth0-react-thin 2.19.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/LICENSE +21 -0
- package/README.md +185 -0
- package/dist/auth-state.d.ts +15 -0
- package/dist/auth-state.d.ts.map +1 -0
- package/dist/auth0-context.d.ts +423 -0
- package/dist/auth0-context.d.ts.map +1 -0
- package/dist/auth0-provider.d.ts +96 -0
- package/dist/auth0-provider.d.ts.map +1 -0
- package/dist/auth0-react.cjs.js +830 -0
- package/dist/auth0-react.cjs.js.map +1 -0
- package/dist/auth0-react.esm.js +723 -0
- package/dist/auth0-react.esm.js.map +1 -0
- package/dist/auth0-react.js +759 -0
- package/dist/auth0-react.js.map +1 -0
- package/dist/auth0-react.min.js +2 -0
- package/dist/auth0-react.min.js.map +1 -0
- package/dist/errors.d.ts +12 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/reducer.d.ts +19 -0
- package/dist/reducer.d.ts.map +1 -0
- package/dist/use-auth0.d.ts +29 -0
- package/dist/use-auth0.d.ts.map +1 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/with-auth0.d.ts +29 -0
- package/dist/with-auth0.d.ts.map +1 -0
- package/dist/with-authentication-required.d.ts +77 -0
- package/dist/with-authentication-required.d.ts.map +1 -0
- package/package.json +43 -0
- package/src/auth-state.tsx +21 -0
- package/src/auth0-context.tsx +506 -0
- package/src/auth0-provider.tsx +488 -0
- package/src/errors.tsx +14 -0
- package/src/index.tsx +100 -0
- package/src/reducer.tsx +59 -0
- package/src/use-auth0.tsx +34 -0
- package/src/utils.tsx +74 -0
- package/src/with-auth0.tsx +44 -0
- package/src/with-authentication-required.tsx +141 -0
package/src/utils.tsx
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { OAuthError } from './errors';
|
|
2
|
+
|
|
3
|
+
const CODE_RE = /[?&](?:connect_)?code=[^&]+/;
|
|
4
|
+
const STATE_RE = /[?&]state=[^&]+/;
|
|
5
|
+
const ERROR_RE = /[?&]error=[^&]+/;
|
|
6
|
+
|
|
7
|
+
interface WithError {
|
|
8
|
+
error: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface WithErrorAndDescription {
|
|
12
|
+
error: string;
|
|
13
|
+
error_description: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const hasAuthParams = (searchParams = window.location.search): boolean =>
|
|
17
|
+
(CODE_RE.test(searchParams) || ERROR_RE.test(searchParams)) &&
|
|
18
|
+
STATE_RE.test(searchParams);
|
|
19
|
+
|
|
20
|
+
const normalizeErrorFn =
|
|
21
|
+
(fallbackMessage: string) =>
|
|
22
|
+
(error: unknown): Error => {
|
|
23
|
+
if (error instanceof Error) {
|
|
24
|
+
return error;
|
|
25
|
+
}
|
|
26
|
+
// try to check errors of the following form: {error: string; error_description?: string}
|
|
27
|
+
if (
|
|
28
|
+
error !== null &&
|
|
29
|
+
typeof error === 'object' &&
|
|
30
|
+
'error' in error &&
|
|
31
|
+
typeof (error as WithError).error === 'string'
|
|
32
|
+
) {
|
|
33
|
+
if (
|
|
34
|
+
'error_description' in error &&
|
|
35
|
+
typeof (error as WithErrorAndDescription).error_description === 'string'
|
|
36
|
+
) {
|
|
37
|
+
const e = error as WithErrorAndDescription;
|
|
38
|
+
return new OAuthError(e.error, e.error_description);
|
|
39
|
+
}
|
|
40
|
+
const e = error as WithError;
|
|
41
|
+
return new OAuthError(e.error);
|
|
42
|
+
}
|
|
43
|
+
return new Error(fallbackMessage);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const loginError = normalizeErrorFn('Login failed');
|
|
47
|
+
|
|
48
|
+
export const tokenError = normalizeErrorFn('Get access token failed');
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @ignore
|
|
52
|
+
* Helper function to map the v1 `redirectUri` option to the v2 `authorizationParams.redirect_uri`
|
|
53
|
+
* and log a warning.
|
|
54
|
+
*/
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
56
|
+
export const deprecateRedirectUri = (options?: any) => {
|
|
57
|
+
if (options?.redirectUri) {
|
|
58
|
+
console.warn(
|
|
59
|
+
'Using `redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `redirectUri` will be no longer supported in a future version'
|
|
60
|
+
);
|
|
61
|
+
options.authorizationParams = options.authorizationParams ?? {};
|
|
62
|
+
options.authorizationParams.redirect_uri = options.redirectUri;
|
|
63
|
+
delete options.redirectUri;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (options?.authorizationParams?.redirectUri) {
|
|
67
|
+
console.warn(
|
|
68
|
+
'Using `authorizationParams.redirectUri` has been deprecated, please use `authorizationParams.redirect_uri` instead as `authorizationParams.redirectUri` will be removed in a future version'
|
|
69
|
+
);
|
|
70
|
+
options.authorizationParams.redirect_uri =
|
|
71
|
+
options.authorizationParams.redirectUri;
|
|
72
|
+
delete options.authorizationParams.redirectUri;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import React, { ComponentType } from 'react';
|
|
2
|
+
import Auth0Context, { Auth0ContextInterface } from './auth0-context';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Components wrapped in `withAuth0` will have an additional `auth0` prop
|
|
6
|
+
*/
|
|
7
|
+
export interface WithAuth0Props {
|
|
8
|
+
auth0: Auth0ContextInterface;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* ```jsx
|
|
13
|
+
* class MyComponent extends Component {
|
|
14
|
+
* render() {
|
|
15
|
+
* // Access the auth context from the `auth0` prop
|
|
16
|
+
* const { user } = this.props.auth0;
|
|
17
|
+
* return <div>Hello {user.name}!</div>
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
* // Wrap your class component in withAuth0
|
|
21
|
+
* export default withAuth0(MyComponent);
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
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.
|
|
28
|
+
*/
|
|
29
|
+
const withAuth0 = <P extends WithAuth0Props>(
|
|
30
|
+
Component: ComponentType<P>,
|
|
31
|
+
context = Auth0Context
|
|
32
|
+
): ComponentType<Omit<P, keyof WithAuth0Props>> => {
|
|
33
|
+
return function WithAuth(props): React.JSX.Element {
|
|
34
|
+
return (
|
|
35
|
+
<context.Consumer>
|
|
36
|
+
{(auth: Auth0ContextInterface): React.JSX.Element => (
|
|
37
|
+
<Component {...(props as P)} auth0={auth} />
|
|
38
|
+
)}
|
|
39
|
+
</context.Consumer>
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default withAuth0;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import React, { ComponentType, useEffect, FC } from 'react';
|
|
2
|
+
import useAuth0 from './use-auth0';
|
|
3
|
+
import Auth0Context, {
|
|
4
|
+
Auth0ContextInterface,
|
|
5
|
+
RedirectLoginOptions,
|
|
6
|
+
} from './auth0-context';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @ignore
|
|
10
|
+
*/
|
|
11
|
+
const defaultOnRedirecting = (): React.JSX.Element => <></>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @ignore
|
|
15
|
+
*/
|
|
16
|
+
const defaultOnBeforeAuthentication = async (): Promise<void> => {/* noop */ };
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @ignore
|
|
20
|
+
*/
|
|
21
|
+
const defaultReturnTo = (): string =>
|
|
22
|
+
`${window.location.pathname}${window.location.search}`;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Options for the withAuthenticationRequired Higher Order Component
|
|
26
|
+
*/
|
|
27
|
+
export interface WithAuthenticationRequiredOptions {
|
|
28
|
+
/**
|
|
29
|
+
* ```js
|
|
30
|
+
* withAuthenticationRequired(Profile, {
|
|
31
|
+
* returnTo: '/profile'
|
|
32
|
+
* })
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* or
|
|
36
|
+
*
|
|
37
|
+
* ```js
|
|
38
|
+
* withAuthenticationRequired(Profile, {
|
|
39
|
+
* returnTo: () => window.location.hash.substr(1)
|
|
40
|
+
* })
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* Add a path for the `onRedirectCallback` handler to return the user to after login.
|
|
44
|
+
*/
|
|
45
|
+
returnTo?: string | (() => string);
|
|
46
|
+
/**
|
|
47
|
+
* ```js
|
|
48
|
+
* withAuthenticationRequired(Profile, {
|
|
49
|
+
* onRedirecting: () => <div>Redirecting you to the login...</div>
|
|
50
|
+
* })
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* Render a message to show that the user is being redirected to the login.
|
|
54
|
+
*/
|
|
55
|
+
onRedirecting?: () => React.JSX.Element;
|
|
56
|
+
/**
|
|
57
|
+
* ```js
|
|
58
|
+
* withAuthenticationRequired(Profile, {
|
|
59
|
+
* onBeforeAuthentication: () => { analyticsLibrary.track('login_triggered'); }
|
|
60
|
+
* })
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* Allows executing logic before the user is redirected to the login page.
|
|
64
|
+
*/
|
|
65
|
+
onBeforeAuthentication?: () => Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* ```js
|
|
68
|
+
* withAuthenticationRequired(Profile, {
|
|
69
|
+
* loginOptions: {
|
|
70
|
+
* appState: {
|
|
71
|
+
* customProp: 'foo'
|
|
72
|
+
* }
|
|
73
|
+
* }
|
|
74
|
+
* })
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* Pass additional login options, like extra `appState` to the login page.
|
|
78
|
+
* This will be merged with the `returnTo` option used by the `onRedirectCallback` handler.
|
|
79
|
+
*/
|
|
80
|
+
loginOptions?: RedirectLoginOptions;
|
|
81
|
+
/**
|
|
82
|
+
* The context to be used when calling useAuth0, this should only be provided if you are using multiple Auth0Providers
|
|
83
|
+
* within your application and you wish to tie a specific component to a Auth0Provider other than the Auth0Provider
|
|
84
|
+
* associated with the default Auth0Context.
|
|
85
|
+
*/
|
|
86
|
+
context?: React.Context<Auth0ContextInterface>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* ```js
|
|
91
|
+
* const MyProtectedComponent = withAuthenticationRequired(MyComponent);
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* When you wrap your components in this Higher Order Component and an anonymous user visits your component
|
|
95
|
+
* they will be redirected to the login page; after login they will be returned to the page they were redirected from.
|
|
96
|
+
*/
|
|
97
|
+
const withAuthenticationRequired = <P extends object>(
|
|
98
|
+
Component: ComponentType<P>,
|
|
99
|
+
options: WithAuthenticationRequiredOptions = {}
|
|
100
|
+
): FC<P> => {
|
|
101
|
+
return function WithAuthenticationRequired(props: P): React.JSX.Element {
|
|
102
|
+
const {
|
|
103
|
+
returnTo = defaultReturnTo,
|
|
104
|
+
onRedirecting = defaultOnRedirecting,
|
|
105
|
+
onBeforeAuthentication = defaultOnBeforeAuthentication,
|
|
106
|
+
loginOptions,
|
|
107
|
+
context = Auth0Context,
|
|
108
|
+
} = options;
|
|
109
|
+
|
|
110
|
+
const { isAuthenticated, isLoading, loginWithRedirect } =
|
|
111
|
+
useAuth0(context);
|
|
112
|
+
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
if (isLoading || isAuthenticated) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const opts = {
|
|
118
|
+
...loginOptions,
|
|
119
|
+
appState: {
|
|
120
|
+
...loginOptions?.appState,
|
|
121
|
+
returnTo: typeof returnTo === 'function' ? returnTo() : returnTo,
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
void (async (): Promise<void> => {
|
|
125
|
+
await onBeforeAuthentication();
|
|
126
|
+
await loginWithRedirect(opts);
|
|
127
|
+
})();
|
|
128
|
+
}, [
|
|
129
|
+
isLoading,
|
|
130
|
+
isAuthenticated,
|
|
131
|
+
loginWithRedirect,
|
|
132
|
+
onBeforeAuthentication,
|
|
133
|
+
loginOptions,
|
|
134
|
+
returnTo,
|
|
135
|
+
]);
|
|
136
|
+
|
|
137
|
+
return isAuthenticated ? <Component {...props} /> : onRedirecting();
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export default withAuthenticationRequired;
|