The user can logout himself when he/she clicks on the logout button but if the token is expired he/she cant logout because in my application, the token is used in both serve
You need to wrap the Main component with a HOC. The HOC will validate the token and if OK allow the component to display. If the token is invalid, the login page is redirected to.
const authChecker = (Component) => {
return class extends React.Component {
state = {
show: false;
}
componentWillReceiveProps(nextProps) {
if (nextProps.children !== this.props.children) {
this.checkAuth();
}
}
componentDidMount() {
this.checkAuth();
}
checkAuth() {
Api.checkAuth()
.then(result => {
if (result.success) {
this.setState({ show: true });
} else {
// logout since token expired
API.logout();
}
});
}
render() {
return this.state.show &&
}
}
}
export default authChecker(Main);