Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TUser type param to useAuth0 hook #230

Merged
merged 4 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function App() {
user,
loginWithRedirect,
logout,
} = useAuth0();
} = useAuth0<{ name: string }>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} = useAuth0<{ name: string }>();
} = useAuth0();

The README examples are plain js


if (isLoading) {
return <div>Loading...</div>;
Expand Down
4 changes: 3 additions & 1 deletion examples/cra-react-router/src/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { useHistory, Link } from 'react-router-dom';
import { useAuth0 } from '@auth0/auth0-react';

export function Nav() {
const { isAuthenticated, user, loginWithRedirect, logout } = useAuth0();
const { isAuthenticated, user, loginWithRedirect, logout } = useAuth0<{
name: string;
}>();
const history = useHistory();
const [pathname, setPathname] = useState(() => history.location.pathname);

Expand Down
4 changes: 2 additions & 2 deletions src/auth-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ export type User = any; // eslint-disable-line @typescript-eslint/no-explicit-an
/**
* The auth state which, when combined with the auth methods, make up the return object of the `useAuth0` hook.
*/
export interface AuthState {
export interface AuthState<TUser extends User = User> {
error?: Error;
isAuthenticated: boolean;
isLoading: boolean;
user?: User;
user?: TUser;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/auth0-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
RedirectLoginOptions as Auth0RedirectLoginOptions,
} from '@auth0/auth0-spa-js';
import { createContext } from 'react';
import { AuthState, initialAuthState } from './auth-state';
import { AuthState, initialAuthState, User } from './auth-state';

export interface RedirectLoginOptions extends BaseLoginOptions {
/**
Expand All @@ -36,7 +36,8 @@ export interface RedirectLoginOptions extends BaseLoginOptions {
/**
* Contains the authenticated state and authentication methods provided by the `useAuth0` hook.
*/
export interface Auth0ContextInterface extends AuthState {
export interface Auth0ContextInterface<TUser extends User = User>
extends AuthState<TUser> {
/**
* ```js
* const token = await getAccessTokenSilently(options);
Expand Down
8 changes: 6 additions & 2 deletions src/use-auth0.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useContext } from 'react';
import { User } from './auth-state';
import Auth0Context, { Auth0ContextInterface } from './auth0-context';

/**
Expand All @@ -16,11 +17,14 @@ import Auth0Context, { Auth0ContextInterface } from './auth0-context';
* loginWithRedirect,
* loginWithPopup,
* logout,
* } = useAuth0();
* } = useAuth0<TUser>();
* ```
*
* Use the `useAuth0` hook in your components to access the auth state and methods.
*
* TUser is an optional type param to provide a type to the `user` field.
*/
const useAuth0 = (): Auth0ContextInterface => useContext(Auth0Context);
const useAuth0 = <TUser extends User = User>(): Auth0ContextInterface<TUser> =>
useContext(Auth0Context);

export default useAuth0;