-1
I’m new to React and I’m trying to implement a route control, so I created an Auth file:
const Auth = {
isAdmin: false,
authenticate(){
this.isAdmin = true
},
signout(){
this.isAdmin = false
},
getAuth(){
return this.isAdmin;
}
}
export default Auth
When I log in and logout everything happens accordingly, in the login isAdmin becomes true and in the logout becomes false. But in the if ternary to show the logout option does not work, it is as if it did not make the comparison. But when printing the isAdmin value, the correct value appears:
{Auth.getAuth() ?( <li className="nav-item active">
<a href="#" onClick={logout} style={{ textDecoration: "none" }}>
<p className="nav-link">Log Out</p>
</a>
</li>) : null}
but even with isAdmin being true, this li is not rendered, and so is protected route control, I can never access it even though isAdmin is true:
function App() {
return (
<div>
<div className="App">
<Header />
<Switch>
<Route exact path="/" component={PostInfo} />
<Route path="/register" component={Register} />
<Route path="/login" component={Login} />
<Route path="/post/:slug" component={Post} />
<PrivateRoute path="/add" component={Add} />
</Switch>
</div>
</div>
);
}
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={(props) =>
Auth.getAuth() ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: "/",
}}
/>
)
}
/>
);
I can’t find a problem with the code... and I’ve done a lot of research but everything seems to be right
Nobody knows how to help or point a direction
– Thiago