@auth0/auth0-react 1.4.0 → 1.8.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/dist/index.d.ts CHANGED
@@ -3,6 +3,6 @@ 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
5
  export { default as Auth0Context, Auth0ContextInterface, RedirectLoginOptions, } from './auth0-context';
6
- export { PopupLoginOptions, PopupConfigOptions, GetIdTokenClaimsOptions, GetTokenWithPopupOptions, LogoutOptions, LogoutUrlOptions, CacheLocation, GetTokenSilentlyOptions, IdToken, } from '@auth0/auth0-spa-js';
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,GACR,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,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"}
package/dist/reducer.d.ts CHANGED
@@ -1,8 +1,9 @@
1
- import { AuthState, User } from './auth-state';
1
+ import { User } from '@auth0/auth0-spa-js';
2
+ import { AuthState } from './auth-state';
2
3
  declare type Action = {
3
4
  type: 'LOGIN_POPUP_STARTED';
4
5
  } | {
5
- type: 'INITIALISED' | 'LOGIN_POPUP_COMPLETE' | 'GET_ACCESS_TOKEN_COMPLETE';
6
+ type: 'INITIALISED' | 'LOGIN_POPUP_COMPLETE' | 'GET_ACCESS_TOKEN_COMPLETE' | 'HANDLE_REDIRECT_COMPLETE';
6
7
  user?: User;
7
8
  } | {
8
9
  type: 'LOGOUT';
@@ -13,6 +14,6 @@ declare type Action = {
13
14
  /**
14
15
  * Handles how that state changes in the `useAuth0` hook.
15
16
  */
16
- export declare const reducer: (state: AuthState, action: Action) => AuthState;
17
+ export declare const reducer: (state: AuthState<User>, action: Action) => AuthState<User>;
17
18
  export {};
18
19
  //# sourceMappingURL=reducer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"reducer.d.ts","sourceRoot":"","sources":["../src/reducer.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAE/C,aAAK,MAAM,GACP;IAAE,IAAI,EAAE,qBAAqB,CAAA;CAAE,GAC/B;IACE,IAAI,EACA,aAAa,GACb,sBAAsB,GACtB,2BAA2B,CAAC;IAChC,IAAI,CAAC,EAAE,IAAI,CAAC;CACb,GACD;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC;AAEpC;;GAEG;AACH,eAAO,MAAM,OAAO,iDAsCnB,CAAC"}
1
+ {"version":3,"file":"reducer.d.ts","sourceRoot":"","sources":["../src/reducer.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,aAAK,MAAM,GACP;IAAE,IAAI,EAAE,qBAAqB,CAAA;CAAE,GAC/B;IACE,IAAI,EACA,aAAa,GACb,sBAAsB,GACtB,2BAA2B,GAC3B,0BAA0B,CAAC;IAC/B,IAAI,CAAC,EAAE,IAAI,CAAC;CACb,GACD;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,KAAK,CAAA;CAAE,CAAC;AAEpC;;GAEG;AACH,eAAO,MAAM,OAAO,6DAuCnB,CAAC"}
@@ -1,3 +1,4 @@
1
+ import { User } from '@auth0/auth0-spa-js';
1
2
  import { Auth0ContextInterface } from './auth0-context';
2
3
  /**
3
4
  * ```js
@@ -14,11 +15,13 @@ import { Auth0ContextInterface } from './auth0-context';
14
15
  * loginWithRedirect,
15
16
  * loginWithPopup,
16
17
  * logout,
17
- * } = useAuth0();
18
+ * } = useAuth0<TUser>();
18
19
  * ```
19
20
  *
20
21
  * Use the `useAuth0` hook in your components to access the auth state and methods.
22
+ *
23
+ * TUser is an optional type param to provide a type to the `user` field.
21
24
  */
22
- declare const useAuth0: () => Auth0ContextInterface;
25
+ declare const useAuth0: <TUser extends User = User>() => Auth0ContextInterface<TUser>;
23
26
  export default useAuth0;
24
27
  //# 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,OAAqB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAEtE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,QAAA,MAAM,QAAQ,6BAAwD,CAAC;AAEvE,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,+DAC4C,CAAC;AAE3D,eAAe,QAAQ,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { RedirectLoginOptions } from '@auth0/auth0-spa-js';
2
+ import { RedirectLoginOptions, User } from '@auth0/auth0-spa-js';
3
3
  /**
4
4
  * Options for the withAuthenticationRequired Higher Order Component
5
5
  */
@@ -47,6 +47,11 @@ export interface WithAuthenticationRequiredOptions {
47
47
  * This will be merged with the `returnTo` option used by the `onRedirectCallback` handler.
48
48
  */
49
49
  loginOptions?: RedirectLoginOptions;
50
+ /**
51
+ * Check the user object for JWT claims and return a boolean indicating
52
+ * whether or not they are authorized to view the component.
53
+ */
54
+ claimCheck?: (claims?: User) => boolean;
50
55
  }
51
56
  /**
52
57
  * ```js
@@ -1 +1 @@
1
- {"version":3,"file":"with-authentication-required.d.ts","sourceRoot":"","sources":["../src/with-authentication-required.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAuC,MAAM,OAAO,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAc3D;;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;CACrC;AAED;;;;;;;GAOG;AACH,QAAA,MAAM,0BAA0B,mHA8B/B,CAAA;AAED,eAAe,0BAA0B,CAAC"}
1
+ {"version":3,"file":"with-authentication-required.d.ts","sourceRoot":"","sources":["../src/with-authentication-required.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAuC,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,mHA2C/B,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.4.0",
4
+ "version": "1.8.0",
5
5
  "description": "Auth0 SDK for React Single Page Applications (SPA)",
6
6
  "keywords": [
7
7
  "auth0",
@@ -24,7 +24,7 @@
24
24
  "lint": "eslint --ext=tsx ./src ./__tests__",
25
25
  "start": "rollup -cw",
26
26
  "test": "jest --coverage",
27
- "prepack": "npm run test && npm run build",
27
+ "prepack": "npm run build",
28
28
  "docs": "typedoc --options typedoc.js src",
29
29
  "install:examples": "npm i --prefix=examples/cra-react-router --no-package-lock && npm i --prefix=examples/gatsby-app --no-package-lock && npm i --prefix=examples/nextjs-app --no-package-lock && npm ci --prefix=examples/users-api",
30
30
  "start:cra": "npm start --prefix=examples/cra-react-router",
@@ -38,7 +38,7 @@
38
38
  "test:nextjs": "start-server-and-test start:api 3001 start:nextjs 3000 cypress:run",
39
39
  "test:nextjs:watch": "start-server-and-test start:api 3001 start:nextjs 3000 cypress:open",
40
40
  "test:integration": "npm run test:cra && npm run test:gatsby && npm run test:nextjs",
41
- "cypress:run": "cypress run",
41
+ "cypress:run": "cypress run --spec 'cypress/integration/smoke.test.ts'",
42
42
  "cypress:open": "cypress open",
43
43
  "codecov": "codecov"
44
44
  },
@@ -62,22 +62,25 @@
62
62
  "@types/react-dom": "^16.9.6",
63
63
  "@typescript-eslint/eslint-plugin": "^2.30.0",
64
64
  "@typescript-eslint/parser": "^2.30.0",
65
+ "browserstack-cypress-cli": "^1.8.1",
65
66
  "codecov": "^3.7.2",
66
- "cypress": "^4.12.1",
67
+ "cypress": "^7.2.0",
67
68
  "eslint": "^6.8.0",
68
69
  "eslint-plugin-react": "^7.19.0",
69
70
  "eslint-plugin-react-hooks": "^4.0.0",
70
71
  "husky": "^4.2.5",
71
72
  "jest": "^26.6.3",
72
73
  "jest-junit": "^10.0.0",
74
+ "oidc-provider": "^7.6.0",
73
75
  "prettier": "2.0.5",
74
76
  "pretty-quick": "^2.0.1",
75
- "react": "^16.13.1",
76
- "react-dom": "^16.13.1",
77
+ "react": "^16.14.0",
78
+ "react-dom": "^16.14.0",
77
79
  "react-test-renderer": "^16.13.1",
78
80
  "rollup": "^2.7.2",
79
81
  "rollup-plugin-analyzer": "^3.3.0",
80
82
  "rollup-plugin-delete": "^1.2.0",
83
+ "rollup-plugin-dev": "^1.1.3",
81
84
  "rollup-plugin-livereload": "^1.2.0",
82
85
  "rollup-plugin-peer-deps-external": "^2.2.2",
83
86
  "rollup-plugin-serve": "^1.0.1",
@@ -99,6 +102,6 @@
99
102
  }
100
103
  },
101
104
  "dependencies": {
102
- "@auth0/auth0-spa-js": "^1.14.0"
105
+ "@auth0/auth0-spa-js": "^1.18.0"
103
106
  }
104
107
  }
@@ -1,13 +1,13 @@
1
- export type User = any; // eslint-disable-line @typescript-eslint/no-explicit-any
1
+ import { User } from '@auth0/auth0-spa-js';
2
2
 
3
3
  /**
4
4
  * The auth state which, when combined with the auth methods, make up the return object of the `useAuth0` hook.
5
5
  */
6
- export interface AuthState {
6
+ export interface AuthState<TUser extends User = User> {
7
7
  error?: Error;
8
8
  isAuthenticated: boolean;
9
9
  isLoading: boolean;
10
- user?: User;
10
+ user?: TUser;
11
11
  }
12
12
 
13
13
  /**
@@ -9,6 +9,8 @@ import {
9
9
  PopupLoginOptions,
10
10
  PopupConfigOptions,
11
11
  RedirectLoginOptions as Auth0RedirectLoginOptions,
12
+ RedirectLoginResult,
13
+ User,
12
14
  } from '@auth0/auth0-spa-js';
13
15
  import { createContext } from 'react';
14
16
  import { AuthState, initialAuthState } from './auth-state';
@@ -36,7 +38,8 @@ export interface RedirectLoginOptions extends BaseLoginOptions {
36
38
  /**
37
39
  * Contains the authenticated state and authentication methods provided by the `useAuth0` hook.
38
40
  */
39
- export interface Auth0ContextInterface extends AuthState {
41
+ export interface Auth0ContextInterface<TUser extends User = User>
42
+ extends AuthState<TUser> {
40
43
  /**
41
44
  * ```js
42
45
  * const token = await getAccessTokenSilently(options);
@@ -156,6 +159,16 @@ export interface Auth0ContextInterface extends AuthState {
156
159
  * @param options
157
160
  */
158
161
  buildLogoutUrl: (options?: LogoutUrlOptions) => string;
162
+
163
+ /**
164
+ * After the browser redirects back to the callback page,
165
+ * call `handleRedirectCallback` to handle success and error
166
+ * responses from Auth0. If the response is successful, results
167
+ * will be valid according to their expiration times.
168
+ *
169
+ * @param url The URL to that should be used to retrieve the `state` and `code` values. Defaults to `window.location.href` if not given.
170
+ */
171
+ handleRedirectCallback: (url?: string) => Promise<RedirectLoginResult>;
159
172
  }
160
173
 
161
174
  /**
@@ -178,6 +191,7 @@ const initialContext = {
178
191
  loginWithRedirect: stub,
179
192
  loginWithPopup: stub,
180
193
  logout: stub,
194
+ handleRedirectCallback: stub,
181
195
  };
182
196
 
183
197
  /**
@@ -12,6 +12,8 @@ import {
12
12
  GetTokenWithPopupOptions,
13
13
  GetTokenSilentlyOptions,
14
14
  GetIdTokenClaimsOptions,
15
+ RedirectLoginResult,
16
+ ICache,
15
17
  } from '@auth0/auth0-spa-js';
16
18
  import Auth0Context, { RedirectLoginOptions } from './auth0-context';
17
19
  import { hasAuthParams, loginError, tokenError } from './utils';
@@ -89,6 +91,12 @@ export interface Auth0ProviderOptions {
89
91
  * Read more about [changing storage options in the Auth0 docs](https://auth0.com/docs/libraries/auth0-single-page-app-sdk#change-storage-options)
90
92
  */
91
93
  cacheLocation?: CacheLocation;
94
+ /**
95
+ * Specify a custom cache implementation to use for token storage and retrieval. This setting takes precedence over `cacheLocation` if they are both specified.
96
+ *
97
+ * Read more about [creating a custom cache](https://github.com/auth0/auth0-spa-js#creating-a-custom-cache)
98
+ */
99
+ cache?: ICache;
92
100
  /**
93
101
  * If true, refresh tokens are used to fetch new access tokens from the Auth0 server. If false, the legacy technique of using a hidden iframe and the `authorization_code` grant with `prompt=none` is used.
94
102
  * The default setting is `false`.
@@ -130,7 +138,7 @@ export interface Auth0ProviderOptions {
130
138
  */
131
139
  audience?: string;
132
140
  /**
133
- * The Id of an organization to log in to (Organizations is currently a Closed Beta).
141
+ * The Id of an organization to log in to.
134
142
  *
135
143
  * This will specify an `organization` parameter in your user's login request and will add a step to validate
136
144
  * the `org_id` claim in your user's ID Token.
@@ -276,11 +284,15 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
276
284
  );
277
285
 
278
286
  const logout = useCallback(
279
- (opts: LogoutOptions = {}): void => {
280
- client.logout(opts);
287
+ (opts: LogoutOptions = {}): Promise<void> | void => {
288
+ const maybePromise = client.logout(opts);
281
289
  if (opts.localOnly) {
290
+ if (maybePromise && typeof maybePromise.then === 'function') {
291
+ return maybePromise.then(() => dispatch({ type: 'LOGOUT' }));
292
+ }
282
293
  dispatch({ type: 'LOGOUT' });
283
294
  }
295
+ return maybePromise;
284
296
  },
285
297
  [client]
286
298
  );
@@ -330,6 +342,22 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
330
342
  [client]
331
343
  );
332
344
 
345
+ const handleRedirectCallback = useCallback(
346
+ async (url?: string): Promise<RedirectLoginResult> => {
347
+ try {
348
+ return await client.handleRedirectCallback(url);
349
+ } catch (error) {
350
+ throw tokenError(error);
351
+ } finally {
352
+ dispatch({
353
+ type: 'HANDLE_REDIRECT_COMPLETE',
354
+ user: await client.getUser(),
355
+ });
356
+ }
357
+ },
358
+ [client]
359
+ );
360
+
333
361
  return (
334
362
  <Auth0Context.Provider
335
363
  value={{
@@ -342,6 +370,7 @@ const Auth0Provider = (opts: Auth0ProviderOptions): JSX.Element => {
342
370
  loginWithRedirect,
343
371
  loginWithPopup,
344
372
  logout,
373
+ handleRedirectCallback,
345
374
  }}
346
375
  >
347
376
  {children}
package/src/index.tsx CHANGED
@@ -24,5 +24,10 @@ export {
24
24
  CacheLocation,
25
25
  GetTokenSilentlyOptions,
26
26
  IdToken,
27
+ User,
28
+ ICache,
29
+ InMemoryCache,
30
+ LocalStorageCache,
31
+ Cacheable,
27
32
  } from '@auth0/auth0-spa-js';
28
33
  export { OAuthError } from './errors';
package/src/reducer.tsx CHANGED
@@ -1,4 +1,5 @@
1
- import { AuthState, User } from './auth-state';
1
+ import { User } from '@auth0/auth0-spa-js';
2
+ import { AuthState } from './auth-state';
2
3
 
3
4
  type Action =
4
5
  | { type: 'LOGIN_POPUP_STARTED' }
@@ -6,7 +7,8 @@ type Action =
6
7
  type:
7
8
  | 'INITIALISED'
8
9
  | 'LOGIN_POPUP_COMPLETE'
9
- | 'GET_ACCESS_TOKEN_COMPLETE';
10
+ | 'GET_ACCESS_TOKEN_COMPLETE'
11
+ | 'HANDLE_REDIRECT_COMPLETE';
10
12
  user?: User;
11
13
  }
12
14
  | { type: 'LOGOUT' }
@@ -31,6 +33,7 @@ export const reducer = (state: AuthState, action: Action): AuthState => {
31
33
  isLoading: false,
32
34
  error: undefined,
33
35
  };
36
+ case 'HANDLE_REDIRECT_COMPLETE':
34
37
  case 'GET_ACCESS_TOKEN_COMPLETE':
35
38
  if (state.user?.updated_at === action.user?.updated_at) {
36
39
  return state;
package/src/use-auth0.tsx CHANGED
@@ -1,4 +1,5 @@
1
1
  import { useContext } from 'react';
2
+ import { User } from '@auth0/auth0-spa-js';
2
3
  import Auth0Context, { Auth0ContextInterface } from './auth0-context';
3
4
 
4
5
  /**
@@ -16,11 +17,14 @@ import Auth0Context, { Auth0ContextInterface } from './auth0-context';
16
17
  * loginWithRedirect,
17
18
  * loginWithPopup,
18
19
  * logout,
19
- * } = useAuth0();
20
+ * } = useAuth0<TUser>();
20
21
  * ```
21
22
  *
22
23
  * Use the `useAuth0` hook in your components to access the auth state and methods.
24
+ *
25
+ * TUser is an optional type param to provide a type to the `user` field.
23
26
  */
24
- const useAuth0 = (): Auth0ContextInterface => useContext(Auth0Context);
27
+ const useAuth0 = <TUser extends User = User>(): Auth0ContextInterface<TUser> =>
28
+ useContext(Auth0Context) as Auth0ContextInterface<TUser>;
25
29
 
26
- export default useAuth0;
30
+ export default useAuth0;
package/src/utils.tsx CHANGED
@@ -5,8 +5,8 @@ const STATE_RE = /[?&]state=[^&]+/;
5
5
  const ERROR_RE = /[?&]error=[^&]+/;
6
6
 
7
7
  export const hasAuthParams = (searchParams = window.location.search): boolean =>
8
- (CODE_RE.test(searchParams) && STATE_RE.test(searchParams)) ||
9
- ERROR_RE.test(searchParams);
8
+ (CODE_RE.test(searchParams) || ERROR_RE.test(searchParams)) &&
9
+ STATE_RE.test(searchParams);
10
10
 
11
11
  const normalizeErrorFn = (fallbackMessage: string) => (
12
12
  error: Error | { error: string; error_description?: string } | ProgressEvent
@@ -1,5 +1,5 @@
1
1
  import React, { ComponentType, useEffect, FC } from 'react';
2
- import { RedirectLoginOptions } from '@auth0/auth0-spa-js';
2
+ import { RedirectLoginOptions, User } from '@auth0/auth0-spa-js';
3
3
  import useAuth0 from './use-auth0';
4
4
 
5
5
  /**
@@ -60,6 +60,11 @@ export interface WithAuthenticationRequiredOptions {
60
60
  * This will be merged with the `returnTo` option used by the `onRedirectCallback` handler.
61
61
  */
62
62
  loginOptions?: RedirectLoginOptions;
63
+ /**
64
+ * Check the user object for JWT claims and return a boolean indicating
65
+ * whether or not they are authorized to view the component.
66
+ */
67
+ claimCheck?: (claims?: User) => boolean;
63
68
  }
64
69
 
65
70
  /**
@@ -75,15 +80,22 @@ const withAuthenticationRequired = <P extends object>(
75
80
  options: WithAuthenticationRequiredOptions = {}
76
81
  ): FC<P> => {
77
82
  return function WithAuthenticationRequired(props: P): JSX.Element {
78
- const { isAuthenticated, isLoading, loginWithRedirect } = useAuth0();
83
+ const { user, isAuthenticated, isLoading, loginWithRedirect } = useAuth0();
79
84
  const {
80
85
  returnTo = defaultReturnTo,
81
86
  onRedirecting = defaultOnRedirecting,
82
87
  loginOptions = {},
88
+ claimCheck = (): boolean => true,
83
89
  } = options;
84
90
 
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);
96
+
85
97
  useEffect(() => {
86
- if (isLoading || isAuthenticated) {
98
+ if (isLoading || routeIsAuthenticated) {
87
99
  return;
88
100
  }
89
101
  const opts = {
@@ -96,10 +108,16 @@ const withAuthenticationRequired = <P extends object>(
96
108
  (async (): Promise<void> => {
97
109
  await loginWithRedirect(opts);
98
110
  })();
99
- }, [isLoading, isAuthenticated, loginWithRedirect, loginOptions, returnTo]);
111
+ }, [
112
+ isLoading,
113
+ routeIsAuthenticated,
114
+ loginWithRedirect,
115
+ loginOptions,
116
+ returnTo,
117
+ ]);
100
118
 
101
- return isAuthenticated ? <Component {...props} /> : onRedirecting();
119
+ return routeIsAuthenticated ? <Component {...props} /> : onRedirecting();
102
120
  };
103
- }
121
+ };
104
122
 
105
123
  export default withAuthenticationRequired;
package/CHANGELOG.md DELETED
@@ -1,101 +0,0 @@
1
- # Change Log
2
-
3
- ## [v1.4.0](https://github.com/auth0/auth0-react/tree/v1.4.0) (2021-03-26)
4
-
5
- **Added**
6
-
7
- - Update SPA JS, add organizations docs and example [\#211](https://github.com/auth0/auth0-react/pull/211) ([adamjmcgrath](https://github.com/adamjmcgrath))
8
-
9
- **Fixed**
10
-
11
- - Update auth state on access token fail [\#219](https://github.com/auth0/auth0-react/pull/219) ([adamjmcgrath](https://github.com/adamjmcgrath))
12
- - Updates to user should not update memoized getAccessToken* methods [\#213](https://github.com/auth0/auth0-react/pull/213) ([adamjmcgrath](https://github.com/adamjmcgrath))
13
-
14
- ## [v1.3.0](https://github.com/auth0/auth0-react/tree/v1.3.0) (2021-02-16)
15
-
16
- **Added**
17
-
18
- - Added `buildAuthorizeUrl` and `buildLogoutUrl` [\#190](https://github.com/auth0/auth0-react/pull/190) ([THISS](https://github.com/THISS))
19
-
20
- **Changed**
21
-
22
- - `isLoading` should default to `true` even when doing SSR [\#193](https://github.com/auth0/auth0-react/pull/193) ([adamjmcgrath](https://github.com/adamjmcgrath))
23
-
24
- ## [v1.2.0](https://github.com/auth0/auth0-react/tree/v1.2.0) (2020-11-04)
25
-
26
- **Added**
27
-
28
- - [SDK-2106] Memoize auth methods [\#150](https://github.com/auth0/auth0-react/pull/150) ([adamjmcgrath](https://github.com/adamjmcgrath))
29
- - Add react fast refresh support [\#151](https://github.com/auth0/auth0-react/pull/151) ([Idered](https://github.com/Idered))
30
- - [SDK-2105] Skip redirect callback option [\#148](https://github.com/auth0/auth0-react/pull/148) ([adamjmcgrath](https://github.com/adamjmcgrath))
31
- - [SDK-2104] Update peerDeps to account for react 17 [\#147](https://github.com/auth0/auth0-react/pull/147) ([adamjmcgrath](https://github.com/adamjmcgrath))
32
- - [SDK-1938] Update the user/isAuthenticated state after getting a token [\#146](https://github.com/auth0/auth0-react/pull/146) ([adamjmcgrath](https://github.com/adamjmcgrath))
33
- - Add config argument to getAccessTokenWithPopup [\#141](https://github.com/auth0/auth0-react/pull/141) ([ygist](https://github.com/ygist))
34
- - Export AppState type for custom onRedirectCallback's [\#120](https://github.com/auth0/auth0-react/pull/120) ([adamjmcgrath](https://github.com/adamjmcgrath))
35
-
36
- ## [v1.1.0](https://github.com/auth0/auth0-react/tree/v1.1.0) (2020-09-17)
37
-
38
- **Added**
39
-
40
- - [SDK-1927] Check for state param in callback url [\#107](https://github.com/auth0/auth0-react/pull/107) ([adamjmcgrath](https://github.com/adamjmcgrath))
41
- - Support react v16.11 [\#99](https://github.com/auth0/auth0-react/pull/99) ([tiagob](https://github.com/tiagob))
42
-
43
- **Fixed**
44
-
45
- - [SDK-1836] Update state for local logouts [\#81](https://github.com/auth0/auth0-react/pull/81) ([adamjmcgrath](https://github.com/adamjmcgrath))
46
- - Add config argument to loginWithPopup [\#77](https://github.com/auth0/auth0-react/pull/77) ([Aulos](https://github.com/Aulos))
47
-
48
- ## [v1.0.0](https://github.com/auth0/auth0-react/tree/v1.0.0) (2020-06-19)
49
-
50
- **Breaking Change**
51
-
52
- - Allow custom `returnTo` in `withAuthenticationRequired` [\#41](https://github.com/auth0/auth0-react/pull/41) ([adamjmcgrath](https://github.com/adamjmcgrath))
53
-
54
- ## [v0.4.0](https://github.com/auth0/auth0-react/tree/v0.4.0) (2020-06-05)
55
-
56
- **Added**
57
-
58
- - [SDK-1697] Add custom user agent to spa js [\#28](https://github.com/auth0/auth0-react/pull/28) ([adamjmcgrath](https://github.com/adamjmcgrath))
59
- - Use `checkSession` to start login [\#27](https://github.com/auth0/auth0-react/pull/27) ([adamjmcgrath](https://github.com/adamjmcgrath))
60
- - [SDK-1690] Add generated API docs [\#25](https://github.com/auth0/auth0-react/pull/25) ([adamjmcgrath](https://github.com/adamjmcgrath))
61
-
62
- ## [v0.3.1](https://github.com/auth0/auth0-react/tree/v0.3.1) (2020-06-01)
63
-
64
- **Fixed**
65
-
66
- - getToken methods were being called in the wrong scope [\#24](https://github.com/auth0/auth0-react/pull/24) ([adamjmcgrath](https://github.com/adamjmcgrath))
67
-
68
- ## [v0.3.0](https://github.com/auth0/auth0-react/tree/v0.3.0) (2020-05-29)
69
-
70
- **Added**
71
-
72
- - [SDK-1641] Add SSR support [\#17](https://github.com/auth0/auth0-react/pull/17) ([adamjmcgrath](https://github.com/adamjmcgrath))
73
-
74
- **Breaking Changes**
75
-
76
- - [SDK-1694] Camel case props [\#22](https://github.com/auth0/auth0-react/pull/22) ([adamjmcgrath](https://github.com/adamjmcgrath))
77
- - [SDK-1693] Renames some init props [\#21](https://github.com/auth0/auth0-react/pull/21) ([adamjmcgrath](https://github.com/adamjmcgrath))
78
-
79
- ## [v0.2.0](https://github.com/auth0/auth0-react/tree/v0.2.0) (2020-05-20)
80
-
81
- **Added**
82
-
83
- - [SDK-1642] Add missing methods from SPA JS [\#11](https://github.com/auth0/auth0-react/pull/11) ([adamjmcgrath](https://github.com/adamjmcgrath))
84
- - [SDK-1582] Normalize the auth0 error and add error handling to the basic example [\#10](https://github.com/auth0/auth0-react/pull/10) ([adamjmcgrath](https://github.com/adamjmcgrath))
85
-
86
- **Changed**
87
-
88
- - Rename auth prop to prevent clashes and align with public api [\#14](https://github.com/auth0/auth0-react/pull/14) ([adamjmcgrath](https://github.com/adamjmcgrath))
89
- - Bundle SPA JS with the SDK for easier install [\#13](https://github.com/auth0/auth0-react/pull/13) ([adamjmcgrath](https://github.com/adamjmcgrath))
90
-
91
- ## [v0.1.0](https://github.com/auth0/auth0-react/tree/v0.1.0) (2020-05-08)
92
-
93
- **Added**
94
-
95
- - [SDK-1580][SDK-1581] Add withAuth and withLoginRequired [\#7](https://github.com/auth0/auth0-react/pull/7) ([adamjmcgrath](https://github.com/adamjmcgrath))
96
- - [SDK-1583] Setup basic README for Early Access [\#6](https://github.com/auth0/auth0-react/pull/6) ([adamjmcgrath](https://github.com/adamjmcgrath))
97
- - [SDK-1577][SDK-1578] Add login functionality [\#5](https://github.com/auth0/auth0-react/pull/5) ([adamjmcgrath](https://github.com/adamjmcgrath))
98
- - [SDK-1576] Linting [\#4](https://github.com/auth0/auth0-react/pull/4) ([adamjmcgrath](https://github.com/adamjmcgrath))
99
- - [SDK-1572] Added CircleCI config [\#3](https://github.com/auth0/auth0-react/pull/3) ([Widcket](https://github.com/Widcket))
100
- - [SDK-1568] Set up unit tests [\#2](https://github.com/auth0/auth0-react/pull/2) ([adamjmcgrath](https://github.com/adamjmcgrath))
101
- - [SDK-1570][SDK-1571] Setup dev environment and build targets [\#1](https://github.com/auth0/auth0-react/pull/1) ([adamjmcgrath](https://github.com/adamjmcgrath))