1

Hello its my first time here.

I've been trying to solve this issue for the past couple of hours and I cant understand the problem (I'm new to react).

In PrivateRoute I am trying to render an Authentication Component based on a session.

  • session EXISTS => Authentication Component should render
  • NOT EXISTS => a received Component should render

The error I'm getting is:

Objects are not valid as a React child (found: [object Promise]). 
If you meant to render a collection of children, use an array instead.

help will be appreciated!

This is what I have done so far:

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

2
  • Does this error happen regardless of the answer for var auth= await Authorization()? Commented Dec 14, 2018 at 15:27
  • No it generates in this line, I had some difficulties returning a result from Authorization, so I tried this using Promises. and the error accord. Commented Dec 14, 2018 at 15:31

1 Answer 1

2

Found an answer to my post.

I've been doing this wrong.

This is what I came up with so far

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

and works fine!

Sign up to request clarification or add additional context in comments.

1 Comment

I love seeing people answer their own questions. well done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.