使用 react 的授权和身份验证 - 路由器 - dom
Authorization and Authentication using react - router - dom
大家好,我是第一次来
过去几个小时我一直在尝试解决这个问题,但我无法理解这个问题(我是新手)。
在 PrivateRoute
中,我正在尝试根据会话呈现身份验证组件。
- session EXISTS => 身份验证组件应该呈现
- 不存在 => 接收到的组件应该呈现
我得到的错误是:
Objects are not valid as a React child (found: [object Promise]).
If you meant to render a collection of children, use an array instead.
我们将不胜感激!
这是我到目前为止所做的:
import Login from './Login'
import Register from './Register'
import React, {Component} from 'react'
import Authentication from './Authentication'
import { BrowserRouter , Redirect, Route, Switch,} from 'react-router-dom'
const Authorization= ()=>{
return new Promise ((resolve,reject)=>{
fetch('http://localhost:3000/content',{credentials:'include'}) //includes cookie from different port
.then((res)=>res.json())
.then((data)=>{
if (data.user=="notLogged"){
reject(false)
}else
resolve(true)
})
})
}
const PrivateRoute= async ({ component: Component})=> {
var auth= await Authorization()
console.log(auth);
if (auth){
return <Authentication/>
}else{
return <Component/>
}
}
class Index extends Component{
render(){
return(
<BrowserRouter>
{/* router may have only one child element- switch is the more common */}
<Switch>
<PrivateRoute exact path="/" component={Login}/>
<PrivateRoute exact path="/login" component={Login} />
<Route exact path="/register" component={Register} />
</Switch>
</BrowserRouter>
)
}
}
export default Index
找到了我的 post 的答案。
我一直做错了。
这是我到目前为止想出的
import Login from './Login'
import Register from './Register'
import React, {Component} from 'react'
import Authentication from './Authentication'
import { BrowserRouter , Route, Switch} from 'react-router-dom'
const PrivvatRoute=({ component: Component, user})=>(
<Route render={(props) => user==="notLogged" ? <Component/> : <Authentication user={user} /> } />
)
class Index extends Component{
constructor(props){
super(props);
this.state={user:""}
}
render(){
return(
<BrowserRouter>
{/* router may have only one child element- switch is the more common */}
<Switch>
<PrivvatRoute exact path="/" component={Login} user={this.state.user} />
<PrivvatRoute exact path="/login" component={Login} user={this.state.user} />
<PrivvatRoute exact path="/register" component={Register} user={this.state.user} />
</Switch>
</BrowserRouter>
)
}
componentWillMount(){
fetch('http://localhost:3000/content',{credentials:"include"})
.then((data)=>data.json())
.then((data)=>{this.setState(data); console.log(data)
})
}
}
export default Index
并且工作正常!
大家好,我是第一次来
过去几个小时我一直在尝试解决这个问题,但我无法理解这个问题(我是新手)。
在 PrivateRoute
中,我正在尝试根据会话呈现身份验证组件。
- session EXISTS => 身份验证组件应该呈现
- 不存在 => 接收到的组件应该呈现
我得到的错误是:
Objects are not valid as a React child (found: [object Promise]).
If you meant to render a collection of children, use an array instead.
我们将不胜感激!
这是我到目前为止所做的:
import Login from './Login'
import Register from './Register'
import React, {Component} from 'react'
import Authentication from './Authentication'
import { BrowserRouter , Redirect, Route, Switch,} from 'react-router-dom'
const Authorization= ()=>{
return new Promise ((resolve,reject)=>{
fetch('http://localhost:3000/content',{credentials:'include'}) //includes cookie from different port
.then((res)=>res.json())
.then((data)=>{
if (data.user=="notLogged"){
reject(false)
}else
resolve(true)
})
})
}
const PrivateRoute= async ({ component: Component})=> {
var auth= await Authorization()
console.log(auth);
if (auth){
return <Authentication/>
}else{
return <Component/>
}
}
class Index extends Component{
render(){
return(
<BrowserRouter>
{/* router may have only one child element- switch is the more common */}
<Switch>
<PrivateRoute exact path="/" component={Login}/>
<PrivateRoute exact path="/login" component={Login} />
<Route exact path="/register" component={Register} />
</Switch>
</BrowserRouter>
)
}
}
export default Index
找到了我的 post 的答案。
我一直做错了。
这是我到目前为止想出的
import Login from './Login'
import Register from './Register'
import React, {Component} from 'react'
import Authentication from './Authentication'
import { BrowserRouter , Route, Switch} from 'react-router-dom'
const PrivvatRoute=({ component: Component, user})=>(
<Route render={(props) => user==="notLogged" ? <Component/> : <Authentication user={user} /> } />
)
class Index extends Component{
constructor(props){
super(props);
this.state={user:""}
}
render(){
return(
<BrowserRouter>
{/* router may have only one child element- switch is the more common */}
<Switch>
<PrivvatRoute exact path="/" component={Login} user={this.state.user} />
<PrivvatRoute exact path="/login" component={Login} user={this.state.user} />
<PrivvatRoute exact path="/register" component={Register} user={this.state.user} />
</Switch>
</BrowserRouter>
)
}
componentWillMount(){
fetch('http://localhost:3000/content',{credentials:"include"})
.then((data)=>data.json())
.then((data)=>{this.setState(data); console.log(data)
})
}
}
export default Index
并且工作正常!