Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LogoutButton memorizes last visited page #5124

Merged
merged 2 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cypress/integration/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('Create Page', () => {
it('should have a working array input with references', () => {
CreatePage.logout();
LoginPage.login('admin', 'password');
CreatePage.navigate();
CreatePage.waitUntilVisible();
cy.get(CreatePage.elements.addAuthor).click();
cy.get(CreatePage.elements.input('authors[0].user_id')).should(
Expand All @@ -80,6 +81,7 @@ describe('Create Page', () => {
it('should have a working array input with a scoped FormDataConsumer', () => {
CreatePage.logout();
LoginPage.login('admin', 'password');
CreatePage.navigate();
CreatePage.waitUntilVisible();
cy.get(CreatePage.elements.addAuthor).click();
CreatePage.setValues([
Expand Down Expand Up @@ -280,6 +282,7 @@ describe('Create Page', () => {
it('should not reset the form value when switching tabs', () => {
CreatePage.logout();
LoginPage.login('admin', 'password');
CreatePage.navigate();
CreatePage.waitUntilVisible();
UserCreatePage.navigate();

Expand Down
1 change: 1 addition & 0 deletions cypress/integration/tabs-with-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('Tabs with routing', () => {
ShowPage.navigate();
ShowPage.logout();
LoginPage.login('admin', 'password');
ShowPage.navigate();
cy.url().then(url => expect(url).to.contain('#/users'));
});

Expand Down
19 changes: 16 additions & 3 deletions packages/ra-core/src/auth/useLogout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ const useLogout = (): Logout => {
const history = useHistory();

const logout = useCallback(
(params = {}, redirectTo = defaultAuthParams.loginUrl) =>
(
params = {},
redirectTo = defaultAuthParams.loginUrl,
redirectToCurrentLocationAfterLogin = true
) =>
authProvider.logout(params).then(redirectToFromProvider => {
dispatch(clearState());
// redirectTo can contain a query string, e.g '/login?foo=bar'
Expand All @@ -54,7 +58,11 @@ const useLogout = (): Logout => {
const newLocation: LocationDescriptorObject = {
pathname: redirectToParts[0],
};
if (history.location && history.location.pathname) {
if (
redirectToCurrentLocationAfterLogin &&
history.location &&
history.location.pathname
) {
newLocation.state = {
nextPathname: history.location.pathname,
};
Expand Down Expand Up @@ -91,9 +99,14 @@ const useLogout = (): Logout => {
*
* @param {Object} params The parameters to pass to the authProvider
* @param {string} redirectTo The path name to redirect the user to (optional, defaults to login)
* @param {boolean} redirectToCurrentLocationAfterLogin Whether the button shall record the current location to redirect to it after login. true by default.
*
* @return {Promise} The authProvider response
*/
type Logout = (params?: any, redirectTo?: string) => Promise<any>;
type Logout = (
params?: any,
redirectTo?: string,
redirectToCurrentLocationAfterLogin?: boolean
) => Promise<any>;

export default useLogout;
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/auth/Logout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const LogoutWithRef: FunctionComponent<
const translate = useTranslate();
const logout = useLogout();
// eslint-disable-next-line react-hooks/exhaustive-deps
const handleClick = useCallback(() => logout(redirectTo), [
const handleClick = useCallback(() => logout(null, redirectTo, false), [
redirectTo,
logout,
]);
Expand Down