Skip to content

Commit

Permalink
Fix infinite redirection loop in useHandleCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Apr 27, 2023
1 parent e05a6c0 commit b19c1c9
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 59 deletions.
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

0 comments on commit b19c1c9

Please sign in to comment.