当用户未登录时重定向到登录.反应器 [英] When user is not logged in redirect to login. Reactjs
问题描述
我的应用程序看起来像:
class App 扩展组件 {使成为() {<路由器><div><路由精确路径='/login' component={Login}/><路由精确路径='/game' component={GameContainer}/><路由精确路径='/chat' component={ChatContainer}/><路由精确路径='/info' component={InfoContainer}/>
</路由器>}
如果用户访问了/game下的页面并且没有登录,我想将他们重定向到登录页面.
如何在所有路由器中以优雅的方式做到这一点?
看这个答案 https://stackoverflow.com/a/43171515/208079.也许代表比我多的人可以将其标记为重复.
基本思想是使用自定义组件(以下示例中的 PrivateRoute)包装需要身份验证的路由.PrivateRoute 将使用一些逻辑来确定用户是否通过身份验证,然后是;允许请求的路由呈现,或重定向到登录页面.
此链接的 react-router 培训文档中也对此进行了描述 https://reacttraining.com/react-router/web/example/auth-workflow.
这是一个使用上述内容作为灵感的实现.
在 App.js(或你的路由发生的地方)
import React, { Component } from 'react'import { BrowserRouter as Router, Route } from 'react-router-dom'从 './PrivateRoute' 导入 PrivateRoute从'../src/MyComponent' 导入 MyComponent从../src/MyLoginForm"导入 MyLoginForm<路由器><Route path="/login" component={MyLoginForm}/><PrivateRoute path="/onlyAuthorizedAllowedHere/" component={MyComponent}/></路由器>
和 PrivateRoute 组件
//用于判断用户是否通过身份验证//如果他们被允许访问他们导航到的页面.//如果他们是:他们继续到页面//如果不是:它们被重定向到登录页面.从反应"导入反应从./Services/AuthService"导入 AuthService从'react-router-dom'导入{重定向,路由}const PrivateRoute = ({ component: Component, ...rest }) =>{//在下面的行中添加您自己的身份验证.const isLoggedIn = AuthService.isLoggedIn()返回 (<路线{...休息}渲染={道具=>是否已登录?(<组件{...道具}/>) : (<重定向到={{ pathname: '/login', state: { from: props.location } }}/>)}/>)}导出默认的 PrivateRoute
My App looks like:
class App extends Component {
render() {
<Router>
<div>
<Route exact path='/login' component={Login} />
<Route exact path='/game' component={GameContainer} />
<Route exact path='/chat' component={ChatContainer} />
<Route exact path='/info' component={InfoContainer} />
</div>
</Router>
}
If the user visits a page under /game and is not logged in, I want to redirect them to the login page.
How to do it an elegant way in all routers?
See this answer https://stackoverflow.com/a/43171515/208079. Perhaps someone with more rep than me can mark this as a duplicate.
The basic idea is to wrap routes that require authentication with a custom component (PrivateRoute in the example below). PrivateRoute will use some logic to determine if the user is authenticated and then either; allow the requested route to render, or redirect to the login page.
This is also described in the react-router training docs at this link https://reacttraining.com/react-router/web/example/auth-workflow.
Here is an implementation using the above as inspiration.
In App.js (or where your routing is happening)
import React, { Component } from 'react'
import { BrowserRouter as Router, Route } from 'react-router-dom'
import PrivateRoute from './PrivateRoute'
import MyComponent from '../src/MyComponent'
import MyLoginForm from '../src/MyLoginForm'
<Router>
<Route path="/login" component={MyLoginForm} />
<PrivateRoute path="/onlyAuthorizedAllowedHere/" component={MyComponent} />
</Router>
And the PrivateRoute Component
// This is used to determine if a user is authenticated and
// if they are allowed to visit the page they navigated to.
// If they are: they proceed to the page
// If not: they are redirected to the login page.
import React from 'react'
import AuthService from './Services/AuthService'
import { Redirect, Route } from 'react-router-dom'
const PrivateRoute = ({ component: Component, ...rest }) => {
// Add your own authentication on the below line.
const isLoggedIn = AuthService.isLoggedIn()
return (
<Route
{...rest}
render={props =>
isLoggedIn ? (
<Component {...props} />
) : (
<Redirect to={{ pathname: '/login', state: { from: props.location } }} />
)
}
/>
)
}
export default PrivateRoute
这篇关于当用户未登录时重定向到登录.反应器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!