good day
I've read many questions like this How to implement authenticated routes in React Router 4? and I have a question: why it is so hard? I am really confused. I mean COULD I use such simple approach as:
export default function PathRouter() {
return (
<div>
{isAuth && <Switch>
<Route exact path="/devices" component={Devices}/>
...
<Route path="*" component={NotFoundPage}/>
</Switch>}
{!isAuth && <Switch>
<Route exact path="/login" component={Login}/>
<Redirect to="/login" />
</Switch>}
</div>
);
}
where isAuth
is my variable which checks for presence of token in cookies and / or localstorage.
To avoid questions: isAuth could be the function that sends requests to server to check if the token in the cookies is valid.
Could I use that approach or is it wrong? Why do I need private routes? I mean private routes is nothing but a route with an auth constant inside. Or am I wrong?