title: How do I check for token expiration and logout user?
body: <p>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 server side and front end. When user clicks on the logout button, the token from both server and browser is cleared if token is valid. There is a chance that when user does not log out and his/her token expires but is not being cleared in the browser. For addressing this situation, how do I check for token expiration every time the user visits in my app so if the token is expired, clear the token from the browser?</p>
  <p>I tried in saga which watches in the background every time the user refreshes in the page or switch to another page. I don't think this is an efficient way. I reckon middleware comes into play.</p>
  <pre><code>function* loadInitialActions() {
    var dateNow = new Date();
    console.log(jwtDecode(token).exp &lt; dateNow.getTime() - jwtDecode(token).iat);
    const token =
      JSON.parse(localStorage.getItem(&quot;user&quot;)) &amp;&amp;
      JSON.parse(localStorage.getItem(&quot;user&quot;))[&quot;token&quot;];
    if (
      token &amp;&amp;
      jwtDecode(token).exp &lt; dateNow.getTime() - jwtDecode(token).iat
    ) {
      yield put(LOGOUT_SUCCESS);
    }
  }
  
  function* initialize() {
    const watcher = yield fork(loadInitialActions);
    yield take([INITIALIZE_ERROR, INITIALIZE_SUCCESS]);
    yield cancel(watcher);
  }
  
  function* rootSaga() {
    console.log(&quot;rootSaga&quot;);
    yield takeLatest(INITIALIZE, initialize);
  }
  </code></pre>
  <p>So my question is how do I use the token expiration logic and logout user if token is expired from the middleware?</p>