Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/ra-core/src/auth/useHandleAuthCallback.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,28 @@ describe('useHandleAuthCallback', () => {
expect(abort).toHaveBeenCalled();
});
});

it('should only call handleCallback once when the component is rendered', async () => {
const handleCallback = jest.spyOn(authProvider, 'handleCallback');
render(
<React.StrictMode>
<TestMemoryRouter initialEntries={['/auth-callback']}>
<AuthContext.Provider value={authProvider}>
<QueryClientProvider client={new QueryClient()}>
<Routes>
<Route path="/" element={<div>Home</div>} />
<Route
path="/auth-callback"
element={<TestComponent />}
/>
</Routes>
</QueryClientProvider>
</AuthContext.Provider>
</TestMemoryRouter>
</React.StrictMode>
);

await screen.findByText('Home');
expect(handleCallback).toHaveBeenCalledTimes(1);
});
});
19 changes: 13 additions & 6 deletions packages/ra-core/src/auth/useHandleAuthCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,22 @@ export const useHandleAuthCallback = (
const nextSearch = locationState && locationState.nextSearch;
const defaultRedirectUrl = nextPathName ? nextPathName + nextSearch : '/';
const { onSuccess, onError, onSettled, ...queryOptions } = options ?? {};
let handleCallbackPromise: Promise<void> | null;

const queryResult = useQuery({
queryKey: ['auth', 'handleCallback'],
queryFn: ({ signal }) =>
authProvider && typeof authProvider.handleCallback === 'function'
? authProvider
.handleCallback({ signal })
.then(result => result ?? null)
: Promise.resolve(),
queryFn: ({ signal }) => {
if (!handleCallbackPromise) {
handleCallbackPromise =
authProvider &&
typeof authProvider.handleCallback === 'function'
? authProvider
.handleCallback({ signal })
.then(result => result ?? null)
: Promise.resolve();
}
return handleCallbackPromise;
},
retry: false,
...queryOptions,
});
Expand Down
Loading