Skip to content

Commit 66dbf99

Browse files
authored
Merge pull request marmelab#10392 from marmelab/login-getPermissions-cache
Fix `useLogin` should invalidate the `getPermissions` cache
2 parents 53a9715 + 188f594 commit 66dbf99

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import * as React from 'react';
2+
import usePermissions 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+
const permissionQueryParams = {
41+
retry: false,
42+
};
43+
const state = usePermissions({}, permissionQueryParams);
44+
return (
45+
<div>
46+
{state.isPending ? <span>LOADING</span> : null}
47+
{state.permissions ? (
48+
<span>PERMISSIONS: {state.permissions}</span>
49+
) : null}
50+
{state.error ? <span>ERROR</span> : null}
51+
</div>
52+
);
53+
};
54+
55+
export const PermissionsState = () => (
56+
<TestMemoryRouter>
57+
<CoreAdminContext authProvider={getAuthProviderWithLoginAndLogout()}>
58+
<UsePermissions />
59+
<LoginButton />
60+
<LogoutButton />
61+
</CoreAdminContext>
62+
</TestMemoryRouter>
63+
);

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)