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

react-admin always remembers users last visited view. #4971

Closed
WiXSL opened this issue Jun 22, 2020 · 2 comments · Fixed by #5124
Closed

react-admin always remembers users last visited view. #4971

WiXSL opened this issue Jun 22, 2020 · 2 comments · Fixed by #5124
Assignees
Labels

Comments

@WiXSL
Copy link
Contributor

WiXSL commented Jun 22, 2020

You can see this in the demo.
1.- Login
2.- Browse to any view other than the Dashboard.
3.- Log out.
4.- Log in again.

Result: you get redirected to the latest view you visited.
Expected: You should be redirected to the Dashboard.

Changing the Login component doesn't seem to work:

const Login = () => {
    //...
    const login = useLogin();
    const location = useLocation<{ nextPathname: string } | null>();

    const handleSubmit = (auth: FormValues) => {
        setLoading(true);
-       login(auth, location.state ? location.state.nextPathname : '/').catch(
+       login(auth, '/').catch(
            (error: Error) => {
                setLoading(false);
                notify(
                    typeof error === 'string'
                        ? error
                        : typeof error === 'undefined' || !error.message
                        ? 'ra.auth.sign_in_error'
                        : error.message,
                    'warning'
                );
            }
        );
    };
   //...
@WiXSL WiXSL changed the title react-admin always remembers the users last view visited. react-admin always remembers the users last visited view. Jun 22, 2020
@WiXSL
Copy link
Contributor Author

WiXSL commented Jun 22, 2020

The problem seems to be in the useLogin hook.
It always uses the location state.

const useLogin = (): Login => {
    const authProvider = useAuthProvider();
    const location = useLocation();
    const locationState = location.state as any;
    const history = useHistory();
    const nextPathName = locationState && locationState.nextPathname;

    const login = useCallback(
        (params: any = {}, pathName = defaultAuthParams.afterLoginUrl) =>
            authProvider.login(params).then(ret => {
                // --> nextPathName gets used every time if exists
                history.push(nextPathName || pathName);
                return ret;
            }),
        [authProvider, history, nextPathName]
    );

    const loginWithoutProvider = useCallback(
        (_, __) => {
            history.push(defaultAuthParams.afterLoginUrl);
            return Promise.resolve();
        },
        [history]
    );

    return authProvider ? login : loginWithoutProvider;
};

@WiXSL WiXSL changed the title react-admin always remembers the users last visited view. react-admin always remembers users last visited view. Jun 23, 2020
@djhi djhi added the bug label Jun 23, 2020
@fzaninotto
Copy link
Member

That's by design: when a user is logged out bu a 403 response from the server and logs in again, they want to go back to the page they were visiting before.

That means we must distinguish between an automated logout caused by a 403 (which should remember the last page) and a manual logout caused by a click on the "Logout" button (which should not remember the last page).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants