Skip to content

Commit

Permalink
Adding onBeforeAuthentication to the withAuthenticationRequired HOC (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenkelzer committed May 5, 2023
1 parent 29f24a2 commit ba11472
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions __tests__/with-authentication-required.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ describe('withAuthenticationRequired', () => {
);
});

it('should call onBeforeAuthentication before loginWithRedirect', async () => {
const callOrder: string[] = [];
mockClient.getUser.mockResolvedValue(undefined);
mockClient.loginWithRedirect.mockImplementationOnce(async ()=>{ callOrder.push('loginWithRedirect') });
const MyComponent = (): JSX.Element => <>Private</>;
const OnBeforeAuthentication = jest.fn().mockImplementationOnce(async ()=>{ callOrder.push('onBeforeAuthentication') });
const WrappedComponent = withAuthenticationRequired(MyComponent, {
onBeforeAuthentication: OnBeforeAuthentication,
});
render(
<Auth0Provider clientId="__test_client_id__" domain="__test_domain__">
<WrappedComponent />
</Auth0Provider>
);

await waitFor(() => expect(OnBeforeAuthentication).toHaveBeenCalledTimes(1));
await waitFor(() => expect(mockClient.loginWithRedirect).toHaveBeenCalledTimes(1));
await waitFor(() => expect(callOrder).toEqual(['onBeforeAuthentication', 'loginWithRedirect']));
});

it('should pass additional options on to loginWithRedirect', async () => {
mockClient.getUser.mockResolvedValue(undefined);
const MyComponent = (): JSX.Element => <>Private</>;
Expand Down
18 changes: 18 additions & 0 deletions src/with-authentication-required.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import Auth0Context, {
*/
const defaultOnRedirecting = (): JSX.Element => <></>;

/**
* @ignore
*/
const defaultOnBeforeAuthentication = async (): Promise<void> => {/* noop */};

/**
* @ignore
*/
Expand Down Expand Up @@ -48,6 +53,16 @@ export interface WithAuthenticationRequiredOptions {
* Render a message to show that the user is being redirected to the login.
*/
onRedirecting?: () => JSX.Element;
/**
* ```js
* withAuthenticationRequired(Profile, {
* onBeforeAuthentication: () => { analyticsLibrary.track('login_triggered'); }
* })
* ```
*
* Allows executing logic before the user is redirected to the login page.
*/
onBeforeAuthentication?: () => Promise<void>;
/**
* ```js
* withAuthenticationRequired(Profile, {
Expand Down Expand Up @@ -87,6 +102,7 @@ const withAuthenticationRequired = <P extends object>(
const {
returnTo = defaultReturnTo,
onRedirecting = defaultOnRedirecting,
onBeforeAuthentication = defaultOnBeforeAuthentication,
loginOptions,
context = Auth0Context,
} = options;
Expand All @@ -106,12 +122,14 @@ const withAuthenticationRequired = <P extends object>(
},
};
(async (): Promise<void> => {
await onBeforeAuthentication();
await loginWithRedirect(opts);
})();
}, [
isLoading,
isAuthenticated,
loginWithRedirect,
onBeforeAuthentication,
loginOptions,
returnTo,
]);
Expand Down

0 comments on commit ba11472

Please sign in to comment.