admin管理员组文章数量:1335357
Right now I am always using history.replace which never pushes anything on the history stack. I am doing this because if I push and then log out, my app allows using the browser back arrow to go back to the previous route and I don't want that. How can I manipulate the history so that when logout is pressed, I a have nothing on the history stack to go back to.
Right now I am always using history.replace which never pushes anything on the history stack. I am doing this because if I push and then log out, my app allows using the browser back arrow to go back to the previous route and I don't want that. How can I manipulate the history so that when logout is pressed, I a have nothing on the history stack to go back to.
Share Improve this question edited Nov 1, 2019 at 2:29 Vincent D'amour 3,9031 gold badge29 silver badges40 bronze badges asked Oct 15, 2017 at 20:39 Tzvetlin VelevTzvetlin Velev 2,0252 gold badges20 silver badges35 bronze badges2 Answers
Reset to default 4You can't do that with react-router because you should not do that. If the user logout and go back to a page where he/she should not see, a good habit is to display an 403 error page instead. You could show a message saying that the user doesn't have the permission to access the page. This is what most website are doing because it's bad UX to remove browser history.
You can find some 403 page example here.
If for a reason you really want to clear history, you can continue to use .replace
but if the user is still logged in and want to go back, he/she won't be able. You can also open a new tab with your site URL and close the current one, but I don't remend doing that.
THIS WORKED FOR ME !! (Just a small hack)
- Inside your navbar ponent or where you have your logout button, create the following function.
const performRedirect = () => {
if (!is_logged_in) {
return <Redirect push state={is_logged_in} to="/login" />;
}
};
Here I am using the variable to check if a user is logged in. And use Redirect from react-router-dom to push the login path and pass the variable inside the state.
Then call the function inside your render ponent.
{performRedirect()}
This will keep you on the login page even if you click the back button from the browser. Because your ponent continuously renders "/login" because the is_logged_in variable is false.
//Example: NavBar.js
import { Redirect } from "react-router-dom";
const Navbar = () => {
//Redirect function
const performRedirect = () => {
if (!is_logged_in) {
return <Redirect push state={!is_logged_in} to="/login" />;
}
};
return (
<div>
//your nav links
<div>
...
<div>
//call redirect function
{performRedirect()}
</div>
)
}
- ALSO, do the same inside the login page while navigating after a successful login. Because once logged in you should not go back to the login page. Once you get a true response for the login request, do the following.
This will redirect the login page back to "/home" even if you click the back button because inside the Login.js ponent you are redirecting it to the "/home" if it is logged in. you will be able to see the login page only if the user is logged out.
//Example: Login.js
import React from "react";
import { Redirect } from "react-router-dom";
const Login = () => {
//* Redirect function
const performRedirect = () => {
if (is_logged_in) {
if (user_info && user_info.role === "admin") {
return <Redirect to="/admin/dashboard" />;
} else {
return <Redirect push state={is_logged_in} to="/home" />;
}
}
};
return (
<div>
//your login content
<div>
...
<div>
//call redirect function
{performRedirect()}
</div>
)
}
本文标签: javascriptUsing BrowserRouterhow can I clear history on logoutStack Overflow
版权声明:本文标题:javascript - Using BrowserRouter, how can I clear history on logout - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742369701a2461989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论