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

Adding onBeforeAuthentication to the withAuthenticationRequired HOC #534

Merged
merged 2 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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