I am trying to modify the Navbar contents before and after logging in so that before logging in, it shows Login and Register and after logging in, it shows other tabs apart from those two. I have an Auth.js with a function called isAuthenticated() which returns true if the user is logged in or false otherwise. Initially, isAuthenticated() returns false, so the Navbar shows register and login. However, once the user logs in, isAuthenticated() becomes true but that is not reflected in Navbar. How can I change that to ensure that it shows different things upon login/logout.
PS - I havent completed navigation but I can conditionally render it to show different things if isLoggedin is true or false.
App.js
import ProtectedRoute from './protectedroute'
import Auth from './Auth'
class App extends React.Component {
constructor(props) {
super(props);
}
render() {
const isAuthenticated = Auth.isAuthenticated()
console.log(isAuthenticated)
return (
<Router>
<div>
<NavigationBar isLoggedin={isAuthenticated}/>
<Switch>
<Route exact path = '/'
component = {LandingPage}
/>
<Route exact path = '/register'
component = {Register}
/>
<ProtectedRoute exact path = '/Success'
component = {Success}
/>
<Route path="*" component = {() => "404 NOT FOUND"}/>
</Switch>
</div>
</Router>
);
}
}
export default App;
Auth.js
class Auth {
constructor() {
this.authenticated = false;
}
login() {
this.authenticated = true;
}
logout() {
this.authenticated = false;
}
isAuthenticated() {
return this.authenticated;
}
}
export default new Auth();
NavigationBar.js
import React from 'react'
import { Button, Navbar, Nav, NavItem, NavDropdown, MenuItem } from 'react-bootstrap';
import { Link } from 'react-router-dom'
class NavigationBar extends React.Component {
constructor(props) {
super(props);
}
render() {
const isLoggedin = this.props.isLoggedin
console.log(isLoggedin)
return (
<Navbar 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><Link to='/'>Login</Link></Nav.Link>
<Nav.Link><Link to='/register'>Register</Link></Nav.Link>
</Nav>
</Navbar.Collapse>
</Navbar>
)
}
}
export default NavigationBar