Skip to content

Commit 695ebf7

Browse files
Allow passing the Auth0 context to the useAuth hook (#880)
1 parent c5ef74d commit 695ebf7

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

packages/network/src/auth/hooks.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { useAuth0 } from '@auth0/auth0-react';
2-
import { useCallback, useEffect } from 'react';
1+
import { Auth0ContextInterface, useAuth0 } from '@auth0/auth0-react';
2+
import { Context, useCallback, useEffect } from 'react';
33
import { STORAGE_KEY_AUTH_TOKEN, useMonkAppState, useObjectMemo } from '@monkvision/common';
44

55
/**
@@ -12,6 +12,10 @@ export interface UseAuthParams {
1212
* @default true
1313
*/
1414
storeToken?: boolean;
15+
/**
16+
* Optional custom Auth0 context that will be passed through to the `useAuth0` hook.
17+
*/
18+
context?: Context<Auth0ContextInterface>;
1519
}
1620

1721
/**
@@ -43,7 +47,7 @@ const defaultOptions = {
4347
*/
4448
export function useAuth(params?: UseAuthParams): MonkAuthHandle {
4549
const options = { ...defaultOptions, ...(params ?? {}) };
46-
const { getAccessTokenWithPopup, logout } = useAuth0();
50+
const { getAccessTokenWithPopup, logout } = useAuth0(params?.context);
4751
const { setAuthToken } = useMonkAppState();
4852

4953
useEffect(() => {

packages/network/test/auth/hooks.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { STORAGE_KEY_AUTH_TOKEN, useMonkAppState } from '@monkvision/common';
2-
import { useAuth0 } from '@auth0/auth0-react';
2+
import { Auth0ContextInterface, useAuth0 } from '@auth0/auth0-react';
33
import { act, waitFor } from '@testing-library/react';
44
import { renderHook } from '@testing-library/react-hooks';
55
import { useAuth } from '../../src';
6+
import { Context } from 'react';
67

78
describe('Authentication hooks', () => {
89
afterEach(() => {
@@ -116,5 +117,14 @@ describe('Authentication hooks', () => {
116117
unmount();
117118
(useAuth0 as jest.Mock).mockRestore();
118119
});
120+
121+
it('should pass the Auth0 context provided to the useAuth0 hook', async () => {
122+
const context = { test: 'hello' } as unknown as Context<Auth0ContextInterface>;
123+
const { unmount } = renderHook(useAuth, { initialProps: { context } });
124+
125+
expect(useAuth0).toHaveBeenCalledWith(context);
126+
127+
unmount();
128+
});
119129
});
120130
});

0 commit comments

Comments
 (0)