title: Nuxt middleware to check if user is logged in not working
body: <p>I am trying to check if a user is authenticated and redirect them depending on the page they are in. for example if the user is logged in and they try to visit the login or signup page, they should be redirected. I have a middleware for that.</p>
  <p>when I log in the user, the <code>authenticateUser</code> action runs and the user is created, when I check my cookies and local storage on the browser, I see that it is set correctly, but when I visit the login page after logging in, it doesn't redirect me.</p>
  <p>middleware/altauth.js</p>
  <pre><code>export default function (context) {
      console.log(context.store.getters('profile/isAuthenticated'))
      if (context.store.getters.isAuthenticated) {
          context.redirect('/')
      }
  }
  </code></pre>
  <p>also the token is both saved using Cookies and local storage and is persistence is through this middleware
  middleware/checkauth.js</p>
  <pre><code>export default function (context) {
      if(context.hasOwnProperty('ssrContext')) {
          context.store.dispatch('profile/initAuth', context.ssrContext.req);
      } else {
          context.store.dispatch('profile/initAuth', null);
      }
  }
  </code></pre>
  <p>and below are the values for my store</p>
  <pre><code>import Cookie from 'js-cookie';
  
  export const state = () =&gt; ({
      token: null,
  })
  
  export const mutations = {
      setToken(state, token) {
          state.token = token
      },
      clearToken(state) {
          state.token = null
      }
  }
  
  export const actions = {
      async authenticateUser(vuexContext, authData) {
          let authUrl = 'https://look.herokuapp.com/signup/'
          if (authData.isLogin) {
              authUrl = 'https://look.herokuapp.com/login/'
          }
  
          return this.$axios
          .$post(authUrl, authData.form)
          .then(data =&gt; {
              console.log(data);
              const token = data.token
              vuexContext.commit('setToken', token)
              localStorage.setItem(&quot;token&quot;, token)
              Cookie.set('jwt', token);
          })
          .catch(e =&gt; console.log(e))
      },
      initAuth(vuexContext, req) {
          let token
          if (req) {
              if (!req.headers.cookie) {
                  return;
              }
              const jwtCookie = req.headers.cookie
                  .split(';')
                  .find(c =&gt; c.trim().startsWith('jwt='));
              if (!jwtCookie) {
                  return;
              }
              token = jwtCookie.split('=')[1];
          } else {
              token = localStorage.getItem('token');
              if (!token) {
                  return;
              }
          }
          vuexContext.commit('setToken', token);
      }
  }
  
  export const getters = {
      isAuthenticated(state) {
          return state.token != null;
      },
  }
  </code></pre>
  <p>please help, i don't know what the problem can be</p>