Skip to content

Commit 5b27baf

Browse files
authored
Merge pull request #11473 from marmelab/fix/logout-redirect-search
Fix/logout redirect
2 parents 751e5da + a475536 commit 5b27baf

5 files changed

Lines changed: 78 additions & 7 deletions

File tree

‎packages/ra-core/src/auth/useHandleAuthCallback.spec.tsx‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import * as React from 'react';
22
import expect from 'expect';
33
import { render, screen, waitFor } from '@testing-library/react';
4-
import { Route, Routes } from 'react-router-dom';
4+
import { Route, Routes, useLocation } from 'react-router-dom';
55
import { QueryClientProvider, QueryClient } from '@tanstack/react-query';
66

7-
import { useHandleAuthCallback } from './useHandleAuthCallback';
7+
import {
8+
PreviousLocationStorageKey,
9+
useHandleAuthCallback,
10+
} from './useHandleAuthCallback';
811
import { AuthContext } from './AuthContext';
912
import { AuthProvider } from '../types';
1013

@@ -43,6 +46,7 @@ const authProvider: AuthProvider = {
4346
describe('useHandleAuthCallback', () => {
4447
afterEach(() => {
4548
jest.clearAllMocks();
49+
localStorage.removeItem(PreviousLocationStorageKey);
4650
});
4751

4852
it('should redirect to the home route by default when the callback was successfully handled', async () => {
@@ -92,6 +96,33 @@ describe('useHandleAuthCallback', () => {
9296
await screen.findByText('Test');
9397
});
9498

99+
it('should redirect to the previous location without its source param', async () => {
100+
localStorage.setItem(
101+
PreviousLocationStorageKey,
102+
`/test?source=${encodeURIComponent(
103+
JSON.stringify({ title: 'Hello' })
104+
)}&foo=bar`
105+
);
106+
const Test = () => <div>Test{useLocation().search}</div>;
107+
render(
108+
<TestMemoryRouter initialEntries={['/auth-callback']}>
109+
<AuthContext.Provider value={authProvider}>
110+
<QueryClientProvider client={new QueryClient()}>
111+
<Routes>
112+
<Route path="/" element={<div>Home</div>} />
113+
<Route path="/test" element={<Test />} />
114+
<Route
115+
path="/auth-callback"
116+
element={<TestComponent />}
117+
/>
118+
</Routes>
119+
</QueryClientProvider>
120+
</AuthContext.Provider>
121+
</TestMemoryRouter>
122+
);
123+
await screen.findByText('Test?foo=bar');
124+
});
125+
95126
it('should use custom useQuery options such as onError', async () => {
96127
render(
97128
<TestMemoryRouter initialEntries={['/auth-callback']}>

‎packages/ra-core/src/auth/useHandleAuthCallback.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useQuery, UseQueryOptions } from '@tanstack/react-query';
33
import { useRedirect, useLocation } from '../routing';
44
import { AuthRedirectResult } from '../types';
55
import useAuthProvider from './useAuthProvider';
6+
import { removeSourceParam } from './useLogout';
67
import { useEvent } from '../util';
78

89
/**
@@ -54,7 +55,7 @@ export const useHandleAuthCallback = (
5455
);
5556
const redirectTo =
5657
(data as AuthRedirectResult)?.redirectTo ??
57-
previousLocation;
58+
(previousLocation && removeSourceParam(previousLocation));
5859
if (redirectTo === false) {
5960
return;
6061
}

‎packages/ra-core/src/auth/useLogout.spec.tsx‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,18 @@ describe('useLogout', () => {
7272
fireEvent.click(screen.getByText('Logout'));
7373
await screen.findByText('Custom from useLogout caller');
7474
});
75+
it('should not keep the source param for after login', async () => {
76+
render(
77+
<Redirect
78+
redirectTo="default"
79+
search={`?source=${encodeURIComponent(
80+
JSON.stringify({ title: 'Hello' })
81+
)}&foo=bar`}
82+
/>
83+
);
84+
await screen.findByText('Page');
85+
fireEvent.click(screen.getByText('Logout'));
86+
await screen.findByText('Login');
87+
await screen.findByText('Search after login: ?foo=bar');
88+
});
7589
});

‎packages/ra-core/src/auth/useLogout.stories.tsx‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { Route, Routes } from 'react-router';
2+
import { Route, Routes, useLocation } from 'react-router';
33
import { TestMemoryRouter } from '../routing/TestMemoryRouter';
44
import { CoreAdminContext } from '../core/CoreAdminContext';
55
import useLogout from './useLogout';
@@ -10,8 +10,10 @@ export default {
1010

1111
export const Redirect = ({
1212
redirectTo,
13+
search = '',
1314
}: {
1415
redirectTo: 'default' | 'authProvider.logout' | 'caller';
16+
search?: string;
1517
}) => {
1618
const authProvider = {
1719
logout: () =>
@@ -23,11 +25,11 @@ export const Redirect = ({
2325
} as any;
2426

2527
return (
26-
<TestMemoryRouter key={redirectTo}>
28+
<TestMemoryRouter key={redirectTo} initialEntries={[`/${search}`]}>
2729
<CoreAdminContext authProvider={authProvider}>
2830
<Routes>
2931
<Route path="/" element={<div>Page</div>} />
30-
<Route path="/login" element={<div>Login</div>} />
32+
<Route path="/login" element={<LoginPage />} />
3133
<Route
3234
path="/logout_redirect"
3335
element={<div>Custom from authProvider.logout</div>}
@@ -67,6 +69,16 @@ Redirect.argTypes = {
6769
},
6870
};
6971

72+
const LoginPage = () => {
73+
const location = useLocation();
74+
return (
75+
<>
76+
<div>Login</div>
77+
<p>Search after login: {location.state?.nextSearch}</p>
78+
</>
79+
);
80+
};
81+
7082
const LogoutButton = ({ redirectTo }: { redirectTo?: string }) => {
7183
const logout = useLogout();
7284
return (

‎packages/ra-core/src/auth/useLogout.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ const useLogout = (): Logout => {
9797
newLocationOptions = {
9898
state: {
9999
nextPathname: locationRef.current.pathname,
100-
nextSearch: locationRef.current.search,
100+
nextSearch: removeSourceParam(
101+
locationRef.current.search
102+
),
101103
},
102104
};
103105
}
@@ -162,3 +164,14 @@ type Logout = (
162164
) => Promise<any>;
163165

164166
export default useLogout;
167+
168+
// the source param may contain a whole record (e.g. with <CloneButton>), so it isn't kept after login
169+
// accepts a search string or a full URL (where the search may come after the hash)
170+
export const removeSourceParam = (url: string) =>
171+
url.replace(/\?[^#]*/g, search => {
172+
const params = search
173+
.slice(1)
174+
.split('&')
175+
.filter(param => !param.startsWith('source='));
176+
return params.length ? `?${params.join('&')}` : '';
177+
});

0 commit comments

Comments
 (0)