Skip to content

Commit

Permalink
pr 211 suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath committed Mar 25, 2021
1 parent 53d8335 commit 3a8514d
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions src/with-authentication-required.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ const defaultOnRedirecting = (): JSX.Element => <></>;
const defaultReturnTo = (): string =>
`${window.location.pathname}${window.location.search}`;

export interface JWTClaim {
[claim: string]: string | string[];
}

export interface JWTNamespaces {
[namespace: string]: JWTClaim;
}

/**
* Options for the withAuthenticationRequired Higher Order Component
*/
Expand Down Expand Up @@ -73,7 +65,7 @@ export interface WithAuthenticationRequiredOptions {
* Check the user object for JWT claims and return a boolean indicating
* whether or not they are authorized to view the component.
*/
claimCheck?: (claims: JWTNamespaces | User) => boolean;
claimCheck?: (claims: User) => boolean;
}

/**
Expand All @@ -89,28 +81,19 @@ const withAuthenticationRequired = <P extends object>(
options: WithAuthenticationRequiredOptions = {}
): FC<P> => {
return function WithAuthenticationRequired(props: P): JSX.Element {
const {
user = {},
isAuthenticated,
isLoading,
loginWithRedirect,
} = useAuth0();
const { user, isAuthenticated, isLoading, loginWithRedirect } = useAuth0();
const {
returnTo = defaultReturnTo,
onRedirecting = defaultOnRedirecting,
loginOptions = {},
/**
* The claimCheck will return `true` by default.
*/
claimCheck = () => true,
claimCheck = (): boolean => true,
} = options;

/**
* The route is authenticated if the user has valid auth and there are no
* JWT claim mismatches.
*/
const claimsAreAuthenticated = claimCheck(user);
const routeIsAuthenticated = isAuthenticated && claimsAreAuthenticated;
const routeIsAuthenticated = isAuthenticated && claimCheck(user);

useEffect(() => {
if (isLoading || routeIsAuthenticated) {
Expand All @@ -126,7 +109,13 @@ const withAuthenticationRequired = <P extends object>(
(async (): Promise<void> => {
await loginWithRedirect(opts);
})();
}, [isLoading, isAuthenticated, loginWithRedirect, loginOptions, returnTo]);
}, [
isLoading,
routeIsAuthenticated,
loginWithRedirect,
loginOptions,
returnTo,
]);

return routeIsAuthenticated ? <Component {...props} /> : onRedirecting();
};
Expand Down

0 comments on commit 3a8514d

Please sign in to comment.