Skip to content

Commit 9a15c54

Browse files
feat: use User class defined in auth0-spa-js (#236)
* feat: use User class defined in auth0-spa-js * feat: export User class * fix: test Co-authored-by: Tatsushi Kiryu <[email protected]>
1 parent b1bbfc4 commit 9a15c54

File tree

9 files changed

+20
-19
lines changed

9 files changed

+20
-19
lines changed

__tests__/auth-provider.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,12 @@ describe('Auth0Provider', () => {
414414
);
415415
await waitForNextUpdate();
416416

417-
expect(result.current.user.name).toEqual('foo');
417+
expect(result.current.user?.name).toEqual('foo');
418418
clientMock.getUser.mockResolvedValue({ name: 'bar', updated_at: '2' });
419419
await act(async () => {
420420
await result.current.getAccessTokenSilently();
421421
});
422-
expect(result.current.user.name).toEqual('bar');
422+
expect(result.current.user?.name).toEqual('bar');
423423
});
424424

425425
it('should update auth state after getAccessTokenSilently fails', async () => {
@@ -471,12 +471,12 @@ describe('Auth0Provider', () => {
471471
);
472472
await waitForNextUpdate();
473473
const memoized = result.current.getAccessTokenSilently;
474-
expect(result.current.user.name).toEqual('foo');
474+
expect(result.current.user?.name).toEqual('foo');
475475
clientMock.getUser.mockResolvedValue({ name: 'bar', updated_at: '2' });
476476
await act(async () => {
477477
await result.current.getAccessTokenSilently();
478478
});
479-
expect(result.current.user.name).toEqual('bar');
479+
expect(result.current.user?.name).toEqual('bar');
480480
expect(result.current.getAccessTokenSilently).toBe(memoized);
481481
});
482482

__tests__/auth-reducer.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('reducer', () => {
55
it('should initialise when authenticated', async () => {
66
const payload = {
77
isAuthenticated: true,
8-
user: 'Bob',
8+
user: { name: 'Bob' },
99
};
1010
expect(
1111
reducer(initialAuthState, { type: 'INITIALISED', ...payload })

__tests__/with-authentication-required.test.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import '@testing-library/jest-dom/extend-expect';
22
import React from 'react';
33
import withAuthenticationRequired from '../src/with-authentication-required';
44
import { render, screen, waitFor } from '@testing-library/react';
5-
import { Auth0Client } from '@auth0/auth0-spa-js';
5+
import { Auth0Client, User } from '@auth0/auth0-spa-js';
66
import Auth0Provider from '../src/auth0-provider';
77
import { mocked } from 'ts-jest/utils';
8-
import { User } from '../src/auth-state';
98

109
const mockClient = mocked(new Auth0Client({ client_id: '', domain: '' }));
1110

@@ -43,8 +42,8 @@ describe('withAuthenticationRequired', () => {
4342
it('should not allow access to claims-restricted components', async () => {
4443
const MyComponent = (): JSX.Element => <>Private</>;
4544
const WrappedComponent = withAuthenticationRequired(MyComponent, {
46-
claimCheck: (claims: User) =>
47-
claims['https://my.app.io/jwt/roles']?.includes('ADMIN'),
45+
claimCheck: (claims?: User) =>
46+
claims?.['https://my.app.io/jwt/roles']?.includes('ADMIN'),
4847
});
4948
/**
5049
* A user with USER and MODERATOR roles.
@@ -72,8 +71,8 @@ describe('withAuthenticationRequired', () => {
7271
it('should allow access to restricted components when JWT claims present', async () => {
7372
const MyComponent = (): JSX.Element => <>Private</>;
7473
const WrappedComponent = withAuthenticationRequired(MyComponent, {
75-
claimCheck: (claim: User) =>
76-
claim['https://my.app.io/jwt/claims']?.ROLE?.includes('ADMIN'),
74+
claimCheck: (claim?: User) =>
75+
claim?.['https://my.app.io/jwt/claims']?.ROLE?.includes('ADMIN'),
7776
});
7877
/**
7978
* User with ADMIN role.

src/auth-state.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type User = any; // eslint-disable-line @typescript-eslint/no-explicit-any
1+
import { User } from '@auth0/auth0-spa-js';
22

33
/**
44
* The auth state which, when combined with the auth methods, make up the return object of the `useAuth0` hook.

src/auth0-context.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import {
1010
PopupConfigOptions,
1111
RedirectLoginOptions as Auth0RedirectLoginOptions,
1212
RedirectLoginResult,
13+
User,
1314
} from '@auth0/auth0-spa-js';
1415
import { createContext } from 'react';
15-
import { AuthState, initialAuthState, User } from './auth-state';
16+
import { AuthState, initialAuthState } from './auth-state';
1617

1718
export interface RedirectLoginOptions extends BaseLoginOptions {
1819
/**

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ export {
2424
CacheLocation,
2525
GetTokenSilentlyOptions,
2626
IdToken,
27+
User,
2728
} from '@auth0/auth0-spa-js';
2829
export { OAuthError } from './errors';

src/reducer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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';
23

34
type Action =
45
| { type: 'LOGIN_POPUP_STARTED' }

src/use-auth0.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useContext } from 'react';
2-
import { User } from './auth-state';
2+
import { User } from '@auth0/auth0-spa-js';
33
import Auth0Context, { Auth0ContextInterface } from './auth0-context';
44

55
/**
@@ -25,6 +25,6 @@ import Auth0Context, { Auth0ContextInterface } from './auth0-context';
2525
* TUser is an optional type param to provide a type to the `user` field.
2626
*/
2727
const useAuth0 = <TUser extends User = User>(): Auth0ContextInterface<TUser> =>
28-
useContext(Auth0Context);
28+
useContext(Auth0Context) as Auth0ContextInterface<TUser>;
2929

3030
export default useAuth0;

src/with-authentication-required.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { ComponentType, useEffect, FC } from 'react';
2-
import { RedirectLoginOptions } from '@auth0/auth0-spa-js';
2+
import { RedirectLoginOptions, User } from '@auth0/auth0-spa-js';
33
import useAuth0 from './use-auth0';
4-
import { User } from './auth-state';
54

65
/**
76
* @ignore
@@ -65,7 +64,7 @@ export interface WithAuthenticationRequiredOptions {
6564
* Check the user object for JWT claims and return a boolean indicating
6665
* whether or not they are authorized to view the component.
6766
*/
68-
claimCheck?: (claims: User) => boolean;
67+
claimCheck?: (claims?: User) => boolean;
6968
}
7069

7170
/**

0 commit comments

Comments
 (0)