admin管理员组文章数量:1417691
In a React SPA, I have a collection of "pages" under the /src/pages/ folder.
The entry point page is an index.js file under the /src/ folder, where I define a router const like this:
const routing = (
<Router>
<div>
<Switch>
<Route path="/signIn" ponent={SignIn} />
<Route exact path="/" ponent={Homepage} />
<Route path="/page1" ponent={Page1} />
<Route path="/page2" ponent={Page2} />
<Route path="/page3" ponent={Page3} />
<Route ponent={NotFound} />
</Switch>
</div>
</Router>
It works great and all. All pages are navigable like "" and it will render the Page2 React ponent.
Concerns arise when I incorporate user session management (log in, log out). If a user is not logged in the app, all pages should automatically redirect to the /signIn page. And viceversa, if a user is already logged, if the /signIn page is accessed, it should automatically redirect to the root homepage.
Right now I have implemented this by adding the following code to all the pages, right after the render() method is declared in the ponent, like this:
class Page2 extends React.Component {
render() {
if (UserProfile.getUserSessionStatus() !== "logged") {
this.props.history.push("/signIn");
}
}
return (
JSX code to be rendered here...
...
This works, but it feels like a cheap workaround used by someone who is just learning React, not by a professional.
For a proof of concept it works, but I would never dare to use such a thing in a production environment.
So, what's the right, best-practices-aligned way to acplish this?
In a React SPA, I have a collection of "pages" under the /src/pages/ folder.
The entry point page is an index.js file under the /src/ folder, where I define a router const like this:
const routing = (
<Router>
<div>
<Switch>
<Route path="/signIn" ponent={SignIn} />
<Route exact path="/" ponent={Homepage} />
<Route path="/page1" ponent={Page1} />
<Route path="/page2" ponent={Page2} />
<Route path="/page3" ponent={Page3} />
<Route ponent={NotFound} />
</Switch>
</div>
</Router>
It works great and all. All pages are navigable like "https://mysuperapp./page2" and it will render the Page2 React ponent.
Concerns arise when I incorporate user session management (log in, log out). If a user is not logged in the app, all pages should automatically redirect to the /signIn page. And viceversa, if a user is already logged, if the /signIn page is accessed, it should automatically redirect to the root homepage.
Right now I have implemented this by adding the following code to all the pages, right after the render() method is declared in the ponent, like this:
class Page2 extends React.Component {
render() {
if (UserProfile.getUserSessionStatus() !== "logged") {
this.props.history.push("/signIn");
}
}
return (
JSX code to be rendered here...
...
This works, but it feels like a cheap workaround used by someone who is just learning React, not by a professional.
For a proof of concept it works, but I would never dare to use such a thing in a production environment.
So, what's the right, best-practices-aligned way to acplish this?
Share Improve this question asked Feb 16, 2019 at 12:57 Rikai no hōhōRikai no hōhō 8371 gold badge10 silver badges15 bronze badges 1- Possible duplicate of What is the best way to redirect a page using React Router? – Lesair Valmont Commented Feb 23, 2019 at 13:36
1 Answer
Reset to default 5One possible approach is to create a Higher Order ponent(HOC) and use it for protecting any routes that require login.
const PrivateRoute = ({ ponent: Component, ...rest }) => (
<Route {...rest} render={(props) => (
UserProfile.getUserSessionStatus() === "logged"
? <Component {...props} />
: <Redirect to='/login' />
)} />
)
And then use like this
.....
<PrivateRoute path='/page1' ponent={Page1} />
.......
Hope this helps!
本文标签:
版权声明:本文标题:javascript - What's the proper way to redirect with React routing when a user is not logged into the application? - Stac 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745275927a2651185.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论