I am using react router v4 and trying to implement a functionality where no matter what route the user clicks on, he gets taken to the login page i.e. if he is not logged in
Previous answers are not detail enough.
Solution:
{publicRouteList()}
{getLoggedRouteList(state.logged)}
const getLoggedRouteList = (logged) => {
if (!logged) {
return (
{
return (
);
}}
/>
);
}
const output = [];
output.push(
/* Here place route list that need logged */
);
return output;
}
class Login extends React.Component {
login = async param => {
const { location } = this.props;
const { state } = location;
/* Here place request login api */
// go to state.from if set before
if (state && state.from) {
history.replace(state.from);
}
// else go to home
else {
history.replace('/');
}
}
}