Skip to content

Commit 77272cb

Browse files
committed
Fix useLogin should invalidate the getPermissions cache
1 parent 53a9715 commit 77272cb

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { CoreAdminContext } from '../core/CoreAdminContext';
77
import useLogin from './useLogin';
88

99
import { TestMemoryRouter } from '../routing';
10+
import { PermissionsState } from './useLogin.stories';
1011

1112
describe('useLogin', () => {
1213
describe('redirect after login', () => {
@@ -99,4 +100,14 @@ describe('useLogin', () => {
99100
expect(screen.queryByText('Login')).not.toBeNull();
100101
});
101102
});
103+
104+
it('should invalidate the getPermissions cache', async () => {
105+
render(<PermissionsState />);
106+
await screen.findByText('PERMISSIONS: guest');
107+
fireEvent.click(screen.getByText('Login'));
108+
await screen.findByText('PERMISSIONS: admin');
109+
fireEvent.click(screen.getByText('Logout'));
110+
await screen.findByText('LOADING');
111+
await screen.findByText('PERMISSIONS: guest');
112+
});
102113
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import * as React from 'react';
2+
import usePermissions, { UsePermissionsResult } from './usePermissions';
3+
import { CoreAdminContext, TestMemoryRouter, useLogin, useLogout } from '..';
4+
5+
export default {
6+
title: 'ra-core/auth/useLogin',
7+
};
8+
9+
const getAuthProviderWithLoginAndLogout = () => {
10+
let isLoggedIn = false;
11+
return {
12+
login: () => {
13+
isLoggedIn = true;
14+
return Promise.resolve();
15+
},
16+
logout: () => {
17+
isLoggedIn = false;
18+
return Promise.resolve();
19+
},
20+
checkAuth: () => (isLoggedIn ? Promise.resolve() : Promise.reject()),
21+
checkError: () => Promise.reject('bad method'),
22+
getPermissions: () =>
23+
new Promise(resolve =>
24+
setTimeout(resolve, 300, isLoggedIn ? 'admin' : 'guest')
25+
),
26+
};
27+
};
28+
29+
const LoginButton = () => {
30+
const login = useLogin();
31+
return <button onClick={login}>Login</button>;
32+
};
33+
34+
const LogoutButton = () => {
35+
const logout = useLogout();
36+
return <button onClick={logout}>Logout</button>;
37+
};
38+
39+
const UsePermissions = ({
40+
children,
41+
}: {
42+
children: (state: UsePermissionsResult<any, Error>) => React.ReactNode;
43+
}) => {
44+
const permissionQueryParams = {
45+
retry: false,
46+
};
47+
const res = usePermissions({}, permissionQueryParams);
48+
return children(res);
49+
};
50+
51+
const inspectPermissionsState = (state: UsePermissionsResult<any, Error>) => (
52+
<div>
53+
{state.isPending ? <span>LOADING</span> : null}
54+
{state.permissions ? (
55+
<span>PERMISSIONS: {state.permissions}</span>
56+
) : null}
57+
{state.error ? <span>ERROR</span> : null}
58+
</div>
59+
);
60+
61+
export const PermissionsState = () => (
62+
<TestMemoryRouter>
63+
<CoreAdminContext authProvider={getAuthProviderWithLoginAndLogout()}>
64+
<UsePermissions>
65+
{state => inspectPermissionsState(state)}
66+
</UsePermissions>
67+
<LoginButton />
68+
<LogoutButton />
69+
</CoreAdminContext>
70+
</TestMemoryRouter>
71+
);

packages/ra-core/src/auth/useLogin.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useNotificationContext } from '../notification';
55
import { useBasename } from '../routing';
66
import useAuthProvider, { defaultAuthParams } from './useAuthProvider';
77
import { removeDoubleSlashes } from '../routing/useCreatePath';
8+
import { useQueryClient } from '@tanstack/react-query';
89

910
/**
1011
* Get a callback for calling the authProvider.login() method
@@ -31,6 +32,7 @@ import { removeDoubleSlashes } from '../routing/useCreatePath';
3132
*/
3233
const useLogin = (): Login => {
3334
const authProvider = useAuthProvider();
35+
const queryClient = useQueryClient();
3436
const location = useLocation();
3537
const locationState = location.state as any;
3638
const navigate = useNavigate();
@@ -47,6 +49,9 @@ const useLogin = (): Login => {
4749
if (authProvider) {
4850
return authProvider.login(params).then(ret => {
4951
resetNotifications();
52+
queryClient.invalidateQueries({
53+
queryKey: ['auth', 'getPermissions'],
54+
});
5055
if (ret && ret.hasOwnProperty('redirectTo')) {
5156
if (ret) {
5257
navigate(ret.redirectTo);
@@ -67,6 +72,7 @@ const useLogin = (): Login => {
6772
},
6873
[
6974
authProvider,
75+
queryClient,
7076
navigate,
7177
nextPathName,
7278
nextSearch,

0 commit comments

Comments
 (0)