0

I am trying to change the navigation bar contents from Sign In/Register to other things such as Profile once the user logs in. My server sends a 401 when the user is not logged in and I have a HOC (RequireAuth.js) which checks the same for protected routes and redirects them to login if they have not logged in. However, I could not come up with a way to change the navbar contents with this logic and was wondering if there is a good way to do this (I do not want to use Redux for this purpose if possible).

RequireAuth.js

const RequireAuth = ( Component ) => {

    return class Apps extends React.Component {
        state = {
            isAuthenticated: false,
            isLoading: true
        }

        checkAuthentication = async() => {
            const url = '/getinfo'
            const json = await fetch(url, {method: 'GET'})
            if (json.status !== 401)    {
                setTimeout(
                function() {
                    this.setState({isAuthenticated: true, isLoading: false});}.bind(this), 1500);
            } else {
                setTimeout(
                function() {
                    this.setState({isLoading: false});}.bind(this), 1500);
            }
        }

        componentDidMount() {
            this.checkAuthentication()
        }

        render() {
          const style = {position: "fixed", top: "50%", left: "50%", transform: "translate(-50%, -50%)" };
            console.log(this.state.isLoading)
           const { isAuthenticated, isLoading } = this.state;
           if(!isAuthenticated) {
               return this.state.isLoading? <div style={style}><PacmanLoader color={'#36D7CC'}/></div> : <Redirect to="/" />
           }
           return <Component {...this.props} />
        }
    }
}

export { RequireAuth }
class App extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    const NotFoundComponent = () => <div>404 NOT FOUND</div>
  return (
    <div>
      <Router>
            <NavigationBar />
            <Switch>
              <Route exact path = '/'
                component = {LandingPage}
              />
              <Route exact path = '/register'
                component = {Register}
              />
              <Route exact path = '/Profile'
                component = {RequireAuth(Profile)}
              />
              <Route exact path = '/About'
                component = {RequireAuth(About)}
              />
              <Route exact path = '/Name'
                component = {RequireAuth(Name)}
              />
              <Route path="*" component = {NotFoundComponent}/>
            </Switch>
      </Router>
    </div>
  );
}
}

export default withRouter(App);

Navigation.js

class NavigationBar extends React.Component {

  constructor(props) {
    super(props);
  }

render() {
    return (
        <Navbar bg="dark" variant="dark" expand="lg">
          <Navbar.Brand >Hello</Navbar.Brand>
          <Navbar.Toggle aria-controls="basic-navbar-nav" />
          <Navbar.Collapse id="basic-navbar-nav">
            <Nav className="ml-auto">
              <Nav.Link as={Link} to='/'>Login</Nav.Link>
              <Nav.Link as={Link} to='/register'>Register</Nav.Link>
            </Nav>
          </Navbar.Collapse>
        </Navbar>
    )
  }
}

export default withRouter(NavigationBar);
xyz6675
  • 355
  • 2
  • 6
  • 17
  • By "I was having some trouble", are you saying the current code is working now? Because if you're looking to improve working code, you should post on [codereview](https://codereview.stackexchange.com/) instead. If your code *doesn't* work, describe exactly how it fails. –  Jun 09 '20 at 20:49
  • 1
    It works, I just have to implement that feature which I do not know how to – xyz6675 Jun 09 '20 at 21:51
  • The first thing I'd do is lift up the `isAuthenticated` state into the App component; once you do that, you can easily pass that state down as prop to the navigation, or render/redirect conditionally in your routes. –  Jun 09 '20 at 22:08

0 Answers0