Skip to content

Commit 0577a60

Browse files
authored
Merge pull request #10443 from adrien-may/patch-4
Fix `<Authenticated>` briefly renders its children when checkAuth returns error
2 parents 00dd26f + 3483be9 commit 0577a60

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

packages/ra-core/src/auth/Authenticated.spec.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,34 @@ describe('<Authenticated>', () => {
2626
);
2727
await screen.findByText('Loading');
2828
});
29+
30+
it('should not render its child when checkAuth raises an error', async () => {
31+
const NeverDisplayedComponent = jest.fn(() => (
32+
<div>It should not be called</div>
33+
));
34+
35+
const authProvider = {
36+
checkAuth: jest.fn().mockRejectedValue(undefined),
37+
logout: jest.fn().mockResolvedValue(undefined),
38+
} as any;
39+
40+
render(
41+
<CoreAdminContext authProvider={authProvider}>
42+
<Authenticated>
43+
<NeverDisplayedComponent />
44+
</Authenticated>
45+
</CoreAdminContext>
46+
);
47+
48+
// Ensure that the NeverDisplayedComponent is not called
49+
await waitFor(() => {
50+
// Ensure that checkAuth and logout were called as expected
51+
expect(authProvider.checkAuth).toHaveBeenCalled();
52+
expect(authProvider.logout).toHaveBeenCalled();
53+
expect(NeverDisplayedComponent).toHaveBeenCalledTimes(0);
54+
});
55+
});
56+
2957
it('should render its child when authenticated', async () => {
3058
const authProvider = {
3159
login: () => Promise.reject('bad method'),

packages/ra-core/src/auth/Authenticated.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export const Authenticated = (props: AuthenticatedProps) => {
3535
const { authParams, loading = null, children } = props;
3636

3737
// this hook will redirect to login if the user is not authenticated
38-
const { isPending } = useAuthenticated({ params: authParams });
38+
const { isPending, isError } = useAuthenticated({ params: authParams });
3939

40-
if (isPending) {
40+
if (isPending || isError) {
4141
return loading;
4242
}
4343

0 commit comments

Comments
 (0)