title: How to logout once jwt token is expired
body: <p>I am working on a web-app using <code>node.js</code> and <code>vue.js</code>, I am doing authentication and maintaining session using <code>jwt</code> and <code>passport.js</code> using <code>passport-jwtstrategy</code></p>
  
  <p>I have done all the things from creating jwt to protecting routes all the things now my issue is while generating <code>jwt</code> I am passing  <code>expiresIn:3600</code> so I want to auto-logout my user from Ui and remove token from localStorage once it has been one hour</p>
  
  <p>On decoding my <code>jwt</code> I am getting </p>
  
  <pre><code> {
    "name": "Dheeraj",
    "iat": 1571896207,
    "exp": 1571899807
  }
  </code></pre>
  
  <p>So how can I get the real-time when to logout</p>
  
  <p>In my <code>auth.js</code> vue store file my logout code when user clicks on logout is</p>
  
  <pre><code>logout({ commit }) {
          return new Promise((resolve, reject) =&gt; {
              localStorage.removeItem('jwt-token')
              localStorage.removeItem('user-name')
              commit('setAuthUser', null)
              resolve(true)
          })
  
      },
  </code></pre>
  
  <p>In the same file, I have a method <code>getAuthUser</code>  which is running whenever a page is loading or reloading to check to protect rout and guestUser</p>
  
  <pre><code>getAuthUser({ commit, getters }) {
          const authUser = getters['authUser']
          const token = localStorage.getItem('jwt-token')
          const isTokenValid = checkTokenValidity(token)
          if (authUser &amp;&amp; isTokenValid) {
              return Promise.resolve(authUser)
          }
  
          commit('setAuthUser', token)
          commit('setAuthState', true)
          debugger
          return token
  
  
      }
  </code></pre>
  
  <p>So how can I logout once my token is expired
  Anyone out here please guide me how can I logout once the token is expired</p>
  
  <p><strong>Edit</strong></p>
  
  <p>In my router.js file</p>
  
  <pre><code>router.beforeEach((to, from, next) =&gt; {
  store.dispatch('auth/getAuthUser')
      .then((authUser) =&gt; {
          const isAuthenticated = store.getters['auth/isAuthenticated']
  
          if (to.meta.onlyAuthUser) {
              if (isAuthenticated) {
                  next()
              } else {
                  next({ name: 'login' })
              }
          } else if (to.meta.onlyGuestUser) {
              if (isAuthenticated) {
                  next({ name: 'welcome' })
              } else {
                  next()
              }
          } else {
              next()
          }
      })
  </code></pre>
  
  <p>})</p>
  
  <p>from my auth file I am calling get authUser which I have already mention above</p>
  
  <p>for checking token validity I am using this code</p>
  
  <pre><code>function checkTokenValidity(token) {
  if (token) {
      const decodedToken = jwt.decode(token)
      return decodedToken &amp;&amp; (decodedToken.exp * 1000) &gt; new Date().getTime()
  
  }
  return false
  </code></pre>
  
  <p>}</p>
  
  <p>but it returns false when I am on login page and there is no token there but once I am loged in it shows null</p>
  
  <p><strong>My global api file</strong></p>
  
  <pre><code>    import axios from 'axios';
  
  export default () =&gt; {
      let headers = {
          'cache-control': 'no-cache'
      };
      let accessToken = localStorage.getItem('jwt-token');
  
      if (accessToken &amp;&amp; accessToken !== '') {
          headers.Authorization = accessToken;
  
      };
      return axios.create({
          baseURL: 'http://localhost:8086/',
          headers: headers
      });
  }
  </code></pre>