Skip to content

Fix infinite redirection loop in useHandleCallback #8861

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

Merged
merged 1 commit into from
Apr 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions packages/ra-core/src/auth/useHandleAuthCallback.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,50 +98,6 @@ describe('useHandleAuthCallback', () => {
});
});

it('should logout and not redirect to any page when the callback was not successfully handled', async () => {
const history = createMemoryHistory({ initialEntries: ['/'] });
render(
<HistoryRouter history={history}>
<AuthContext.Provider
value={{
...authProvider,
handleCallback: () => Promise.reject(),
}}
>
<QueryClientProvider client={queryClient}>
<TestComponent />
</QueryClientProvider>
</AuthContext.Provider>
</HistoryRouter>
);
await waitFor(() => {
expect(logout).toHaveBeenCalled();
expect(redirect).not.toHaveBeenCalled();
});
});

it('should redirect to the provided route when the callback was not successfully handled', async () => {
const history = createMemoryHistory({ initialEntries: ['/'] });
render(
<HistoryRouter history={history}>
<AuthContext.Provider
value={{
...authProvider,
handleCallback: () =>
Promise.reject({ redirectTo: '/test' }),
}}
>
<QueryClientProvider client={queryClient}>
<TestComponent />
</QueryClientProvider>
</AuthContext.Provider>
</HistoryRouter>
);
await waitFor(() => {
expect(redirect).toHaveBeenCalledWith('/test');
});
});

it('should use custom useQuery options such as onError', async () => {
const history = createMemoryHistory({ initialEntries: ['/'] });
render(
Expand Down
15 changes: 0 additions & 15 deletions packages/ra-core/src/auth/useHandleAuthCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useLocation } from 'react-router';
import { useRedirect } from '../routing';
import { AuthProvider, AuthRedirectResult } from '../types';
import useAuthProvider from './useAuthProvider';
import useLogout from './useLogout';

/**
* This hook calls the `authProvider.handleCallback()` method on mount. This is meant to be used in a route called
Expand All @@ -17,7 +16,6 @@ export const useHandleAuthCallback = (
) => {
const authProvider = useAuthProvider();
const redirect = useRedirect();
const logout = useLogout();
const location = useLocation();
const locationState = location.state as any;
const nextPathName = locationState && locationState.nextPathname;
Expand Down Expand Up @@ -46,19 +44,6 @@ export const useHandleAuthCallback = (

redirect(redirectTo ?? defaultRedirectUrl);
},
onError: err => {
const { redirectTo = false, logoutOnFailure = true } = (err ??
{}) as AuthRedirectResult;

if (logoutOnFailure) {
logout({}, redirectTo);
}
if (redirectTo === false) {
return;
}

redirect(redirectTo);
},
...options,
}
);
Expand Down