@auth0/auth0-react 1.11.0 → 1.12.1

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/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export { default as Auth0Provider, Auth0ProviderOptions, AppState, } from './aut
2
2
  export { default as useAuth0 } from './use-auth0';
3
3
  export { default as withAuth0, WithAuth0Props } from './with-auth0';
4
4
  export { default as withAuthenticationRequired, WithAuthenticationRequiredOptions, } from './with-authentication-required';
5
- export { default as Auth0Context, Auth0ContextInterface, RedirectLoginOptions, } from './auth0-context';
5
+ export { default as Auth0Context, Auth0ContextInterface, initialContext, RedirectLoginOptions, } from './auth0-context';
6
6
  export { PopupLoginOptions, PopupConfigOptions, GetIdTokenClaimsOptions, GetTokenWithPopupOptions, LogoutOptions, LogoutUrlOptions, CacheLocation, GetTokenSilentlyOptions, IdToken, User, ICache, InMemoryCache, LocalStorageCache, Cacheable, } from '@auth0/auth0-spa-js';
7
7
  export { OAuthError } from './errors';
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,IAAI,aAAa,EACxB,oBAAoB,EACpB,QAAQ,GACT,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,EACL,OAAO,IAAI,0BAA0B,EACrC,iCAAiC,GAClC,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACL,OAAO,IAAI,YAAY,EACvB,qBAAqB,EACrB,oBAAoB,GACrB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,uBAAuB,EACvB,wBAAwB,EACxB,aAAa,EACb,gBAAgB,EAChB,aAAa,EACb,uBAAuB,EACvB,OAAO,EACP,IAAI,EACJ,MAAM,EACN,aAAa,EACb,iBAAiB,EACjB,SAAS,GACV,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,IAAI,aAAa,EACxB,oBAAoB,EACpB,QAAQ,GACT,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,EACL,OAAO,IAAI,0BAA0B,EACrC,iCAAiC,GAClC,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACL,OAAO,IAAI,YAAY,EACvB,qBAAqB,EACrB,cAAc,EACd,oBAAoB,GACrB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,uBAAuB,EACvB,wBAAwB,EACxB,aAAa,EACb,gBAAgB,EAChB,aAAa,EACb,uBAAuB,EACvB,OAAO,EACP,IAAI,EACJ,MAAM,EACN,aAAa,EACb,iBAAiB,EACjB,SAAS,GACV,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC"}
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import { User } from '@auth0/auth0-spa-js';
2
3
  import { Auth0ContextInterface } from './auth0-context';
3
4
  /**
@@ -22,6 +23,6 @@ import { Auth0ContextInterface } from './auth0-context';
22
23
  *
23
24
  * TUser is an optional type param to provide a type to the `user` field.
24
25
  */
25
- declare const useAuth0: <TUser extends User = User>() => Auth0ContextInterface<TUser>;
26
+ declare const useAuth0: <TUser extends User = User>(context?: import("react").Context<Auth0ContextInterface<User>>) => Auth0ContextInterface<TUser>;
26
27
  export default useAuth0;
27
28
  //# sourceMappingURL=use-auth0.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-auth0.d.ts","sourceRoot":"","sources":["../src/use-auth0.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAC3C,OAAqB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAEtE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,QAAA,MAAM,QAAQ,+DAC4C,CAAC;AAE3D,eAAe,QAAQ,CAAC"}
1
+ {"version":3,"file":"use-auth0.d.ts","sourceRoot":"","sources":["../src/use-auth0.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAC3C,OAAqB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAEtE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,QAAA,MAAM,QAAQ,6HAGuC,CAAC;AAEtD,eAAe,QAAQ,CAAC"}
@@ -19,8 +19,11 @@ export interface WithAuth0Props {
19
19
  * export default withAuth0(MyComponent);
20
20
  * ```
21
21
  *
22
- * Wrap your class components in this Higher Order Component to give them access to the Auth0Context
22
+ * Wrap your class components in this Higher Order Component to give them access to the Auth0Context.
23
+ *
24
+ * Providing a context as the second argument allows you to configure the Auth0Provider the Auth0Context
25
+ * should come from f you have multiple within your application.
23
26
  */
24
- declare const withAuth0: <P extends WithAuth0Props>(Component: React.ComponentType<P>) => React.ComponentType<Omit<P, "auth0">>;
27
+ declare const withAuth0: <P extends WithAuth0Props>(Component: React.ComponentType<P>, context?: React.Context<Auth0ContextInterface<import("@auth0/auth0-spa-js").User>>) => React.ComponentType<Omit<P, "auth0">>;
25
28
  export default withAuth0;
26
29
  //# sourceMappingURL=with-auth0.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"with-auth0.d.ts","sourceRoot":"","sources":["../src/with-auth0.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAC7C,OAAqB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAEtE;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,qBAAqB,CAAC;CAC9B;AAED;;;;;;;;;;;;;;GAcG;AACH,QAAA,MAAM,SAAS,wGAYd,CAAC;AAEF,eAAe,SAAS,CAAC"}
1
+ {"version":3,"file":"with-auth0.d.ts","sourceRoot":"","sources":["../src/with-auth0.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAC7C,OAAqB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAEtE;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,qBAAqB,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,QAAA,MAAM,SAAS,4LAad,CAAC;AAEF,eAAe,SAAS,CAAC"}
@@ -1,5 +1,6 @@
1
1
  import React, { ComponentType, FC } from 'react';
2
2
  import { RedirectLoginOptions, User } from '@auth0/auth0-spa-js';
3
+ import { Auth0ContextInterface } from './auth0-context';
3
4
  /**
4
5
  * Options for the withAuthenticationRequired Higher Order Component
5
6
  */
@@ -52,6 +53,12 @@ export interface WithAuthenticationRequiredOptions {
52
53
  * whether or not they are authorized to view the component.
53
54
  */
54
55
  claimCheck?: (claims?: User) => boolean;
56
+ /**
57
+ * The context to be used when calling useAuth0, this should only be provided if you are using multiple Auth0Providers
58
+ * within your application and you wish to tie a specific component to a Auth0Provider other than the Auth0Provider
59
+ * associated with the default Auth0Context.
60
+ */
61
+ context?: React.Context<Auth0ContextInterface>;
55
62
  }
56
63
  /**
57
64
  * ```js
@@ -1 +1 @@
1
- {"version":3,"file":"with-authentication-required.d.ts","sourceRoot":"","sources":["../src/with-authentication-required.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,aAAa,EAAa,EAAE,EAAE,MAAM,OAAO,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAcjE;;GAEG;AACH,MAAM,WAAW,iCAAiC;IAChD;;;;;;;;;;;;;;;;OAgBG;IACH,QAAQ,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,CAAC;IACnC;;;;;;;;OAQG;IACH,aAAa,CAAC,EAAE,MAAM,GAAG,CAAC,OAAO,CAAC;IAClC;;;;;;;;;;;;;OAaG;IACH,YAAY,CAAC,EAAE,oBAAoB,CAAC;IACpC;;;OAGG;IACH,UAAU,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,IAAI,KAAK,OAAO,CAAC;CACzC;AAED;;;;;;;GAOG;AACH,QAAA,MAAM,0BAA0B,kEAErB,iCAAiC,gBAyC3C,CAAC;AAEF,eAAe,0BAA0B,CAAC"}
1
+ {"version":3,"file":"with-authentication-required.d.ts","sourceRoot":"","sources":["../src/with-authentication-required.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,aAAa,EAAa,EAAE,EAAE,MAAM,OAAO,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAEjE,OAAqB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAatE;;GAEG;AACH,MAAM,WAAW,iCAAiC;IAChD;;;;;;;;;;;;;;;;OAgBG;IACH,QAAQ,CAAC,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,CAAC;IACnC;;;;;;;;OAQG;IACH,aAAa,CAAC,EAAE,MAAM,GAAG,CAAC,OAAO,CAAC;IAClC;;;;;;;;;;;;;OAaG;IACH,YAAY,CAAC,EAAE,oBAAoB,CAAC;IACpC;;;OAGG;IACH,UAAU,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,IAAI,KAAK,OAAO,CAAC;IAExC;;;;OAIG;IACH,OAAO,CAAC,EAAE,KAAK,CAAC,OAAO,CAAC,qBAAqB,CAAC,CAAC;CAChD;AAED;;;;;;;GAOG;AACH,QAAA,MAAM,0BAA0B,kEAErB,iCAAiC,gBA4C3C,CAAC;AAEF,eAAe,0BAA0B,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Auth0",
3
3
  "name": "@auth0/auth0-react",
4
- "version": "1.11.0",
4
+ "version": "1.12.1",
5
5
  "description": "Auth0 SDK for React Single Page Applications (SPA)",
6
6
  "keywords": [
7
7
  "auth0",
@@ -53,28 +53,29 @@
53
53
  "devDependencies": {
54
54
  "@rollup/plugin-node-resolve": "^13.1.3",
55
55
  "@rollup/plugin-replace": "^4.0.0",
56
- "@testing-library/jest-dom": "^5.16.3",
57
- "@testing-library/react": "^13.0.0",
56
+ "@testing-library/jest-dom": "^5.16.5",
57
+ "@testing-library/react": "^13.4.0",
58
58
  "@testing-library/react-hooks": "^7.0.2",
59
- "@types/jest": "^27.4.1",
60
- "@types/react": "^17.0.43",
61
- "@types/react-dom": "^17.0.14",
62
- "@typescript-eslint/eslint-plugin": "^5.17.0",
63
- "@typescript-eslint/parser": "^5.17.0",
64
- "browserstack-cypress-cli": "^1.1.2",
65
- "cypress": "^9.5.3",
66
- "eslint": "^8.12.0",
67
- "eslint-plugin-react": "^7.29.4",
68
- "eslint-plugin-react-hooks": "^4.4.0",
69
- "husky": "^4.2.5",
70
- "jest": "^27.5.1",
71
- "jest-junit": "^13.0.0",
59
+ "@types/jest": "^29.2.3",
60
+ "@types/react": "^17.0.52",
61
+ "@types/react-dom": "^17.0.18",
62
+ "@typescript-eslint/eslint-plugin": "^5.45.0",
63
+ "@typescript-eslint/parser": "^5.45.0",
64
+ "browserstack-cypress-cli": "1.8.1",
65
+ "cypress": "^9.7.0",
66
+ "eslint": "^8.28.0",
67
+ "eslint-plugin-react": "^7.31.11",
68
+ "eslint-plugin-react-hooks": "^4.6.0",
69
+ "husky": "^4.3.8",
70
+ "jest": "^29.3.1",
71
+ "jest-environment-jsdom": "^29.3.1",
72
+ "jest-junit": "^15.0.0",
72
73
  "oidc-provider": "^7.10.6",
73
- "prettier": "2.6.2",
74
+ "prettier": "2.8.0",
74
75
  "pretty-quick": "^3.1.3",
75
76
  "react": "^18.2.0",
76
77
  "react-dom": "^18.2.0",
77
- "react-test-renderer": "^18.0.0",
78
+ "react-test-renderer": "^18.2.0",
78
79
  "rollup": "^2.70.1",
79
80
  "rollup-plugin-analyzer": "^4.0.0",
80
81
  "rollup-plugin-delete": "^2.0.0",
@@ -85,7 +86,7 @@
85
86
  "rollup-plugin-terser": "^7.0.2",
86
87
  "rollup-plugin-typescript2": "^0.31.2",
87
88
  "start-server-and-test": "^1.14.0",
88
- "ts-jest": "^27.1.4",
89
+ "ts-jest": "^29.0.3",
89
90
  "tslib": "^2.3.1",
90
91
  "typedoc": "^0.22.13",
91
92
  "typescript": "^4.6.3"
@@ -100,6 +101,6 @@
100
101
  }
101
102
  },
102
103
  "dependencies": {
103
- "@auth0/auth0-spa-js": "^1.22.2"
104
+ "@auth0/auth0-spa-js": "^1.22.6"
104
105
  }
105
106
  }
@@ -66,9 +66,9 @@ export interface Auth0ContextInterface<TUser extends User = User>
66
66
  * the `auth0` cookie.
67
67
  */
68
68
  getAccessTokenSilently: {
69
- (options: GetTokenSilentlyOptions & { detailedResponse: true }): Promise<
70
- GetTokenSilentlyVerboseResponse
71
- >;
69
+ (
70
+ options: GetTokenSilentlyOptions & { detailedResponse: true }
71
+ ): Promise<GetTokenSilentlyVerboseResponse>;
72
72
  (options?: GetTokenSilentlyOptions): Promise<string>;
73
73
  (options: GetTokenSilentlyOptions): Promise<
74
74
  GetTokenSilentlyVerboseResponse | string
@@ -190,7 +190,7 @@ const stub = (): never => {
190
190
  /**
191
191
  * @ignore
192
192
  */
193
- const initialContext = {
193
+ export const initialContext = {
194
194
  ...initialAuthState,
195
195
  buildAuthorizeUrl: stub,
196
196
  buildLogoutUrl: stub,
@@ -21,7 +21,10 @@ import {
21
21
  GetTokenSilentlyOptions,
22
22
  User,
23
23
  } from '@auth0/auth0-spa-js';
24
- import Auth0Context, { RedirectLoginOptions } from './auth0-context';
24
+ import Auth0Context, {
25
+ Auth0ContextInterface,
26
+ RedirectLoginOptions,
27
+ } from './auth0-context';
25
28
  import { hasAuthParams, loginError, tokenError } from './utils';
26
29
  import { reducer } from './reducer';
27
30
  import { initialAuthState } from './auth-state';
@@ -160,6 +163,24 @@ export interface Auth0ProviderOptions {
160
163
  * the Login Widget.
161
164
  */
162
165
  connection?: string;
166
+ /**
167
+ * Context to be used when creating the Auth0Provider, defaults to the internally created context.
168
+ *
169
+ * This allows multiple Auth0Providers to be nested within the same application, the context value can then be
170
+ * passed to useAuth0, withAuth0, or withAuthenticationRequired to use that specific Auth0Provider to access
171
+ * auth state and methods specifically tied to the provider that the context belongs to.
172
+ *
173
+ * When using multiple Auth0Providers in a single application you should do the following to ensure sessions are not
174
+ * overwritten:
175
+ *
176
+ * * Configure a different redirect_uri for each Auth0Provider, and set skipRedirectCallback for each provider to ignore
177
+ * the others redirect_uri
178
+ * * If using localstorage for both Auth0Providers, ensure that the audience and scope are different for so that the key
179
+ * used to store data is different
180
+ *
181
+ * 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)
182
+ */
183
+ context?: React.Context<Auth0ContextInterface>;
163
184
  /**
164
185
  * If you need to send custom parameters to the Authorization Server,
165
186
  * make sure to use the original parameter name.
@@ -236,6 +257,7 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
236
257
  children,
237
258
  skipRedirectCallback,
238
259
  onRedirectCallback = defaultOnRedirectCallback,
260
+ context = Auth0Context,
239
261
  ...clientOpts
240
262
  } = opts;
241
263
  const [client] = useState(
@@ -403,11 +425,7 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
403
425
  handleRedirectCallback,
404
426
  ]);
405
427
 
406
- return (
407
- <Auth0Context.Provider value={contextValue}>
408
- {children}
409
- </Auth0Context.Provider>
410
- );
428
+ return <context.Provider value={contextValue}>{children}</context.Provider>;
411
429
  };
412
430
 
413
431
  export default Auth0Provider;
package/src/index.tsx CHANGED
@@ -12,6 +12,7 @@ export {
12
12
  export {
13
13
  default as Auth0Context,
14
14
  Auth0ContextInterface,
15
+ initialContext,
15
16
  RedirectLoginOptions,
16
17
  } from './auth0-context';
17
18
  export {
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,7 @@
1
1
  import React, { ComponentType, useEffect, FC } from 'react';
2
2
  import { RedirectLoginOptions, User } from '@auth0/auth0-spa-js';
3
3
  import useAuth0 from './use-auth0';
4
+ import Auth0Context, { Auth0ContextInterface } from './auth0-context';
4
5
 
5
6
  /**
6
7
  * @ignore
@@ -65,6 +66,13 @@ export interface WithAuthenticationRequiredOptions {
65
66
  * whether or not they are authorized to view the component.
66
67
  */
67
68
  claimCheck?: (claims?: User) => boolean;
69
+
70
+ /**
71
+ * The context to be used when calling useAuth0, this should only be provided if you are using multiple Auth0Providers
72
+ * within your application and you wish to tie a specific component to a Auth0Provider other than the Auth0Provider
73
+ * associated with the default Auth0Context.
74
+ */
75
+ context?: React.Context<Auth0ContextInterface>;
68
76
  }
69
77
 
70
78
  /**
@@ -80,14 +88,17 @@ const withAuthenticationRequired = <P extends object>(
80
88
  options: WithAuthenticationRequiredOptions = {}
81
89
  ): FC<P> => {
82
90
  return function WithAuthenticationRequired(props: P): JSX.Element {
83
- const { user, isAuthenticated, isLoading, loginWithRedirect } = useAuth0();
84
91
  const {
85
92
  returnTo = defaultReturnTo,
86
93
  onRedirecting = defaultOnRedirecting,
87
94
  claimCheck = (): boolean => true,
88
95
  loginOptions,
96
+ context = Auth0Context,
89
97
  } = options;
90
98
 
99
+ const { user, isAuthenticated, isLoading, loginWithRedirect } =
100
+ useAuth0(context);
101
+
91
102
  /**
92
103
  * The route is authenticated if the user has valid auth and there are no
93
104
  * JWT claim mismatches.