title: Get user on initial page load - Nuxt middleware
body: <p>I'm using Firebase Auth in my Nuxt app and having some trouble with protected routes. I have the nuxt middleware enabled that handles redirection based on whether or not a user a logged in on page navigation.</p>
  <p>My issue is, if you visit a protected route from the address bar and you were previously logged out, you'll be given access to the route. However if you try to visit another protected route from that route you'll be redirected. I am using Vuex to handle the user logged in state.</p>
  <p>How can I get the middleware to kick in on a 'cold start' for a protected route.</p>
  <p>Middleware: <code>router-auth.js</code></p>
  <pre><code>export default function({ store, redirect, route }) {
      const user = JSON.parse(store.state.Authentication.user)
      console.log(user);
      user !== null &amp;&amp; route.name === '/' ? redirect('/dashboard') : ''
      //user === null &amp;&amp; route.name !== '/' ? redirect('/dashboard') : ''
    }
  </code></pre>
  <p>Plugin: <code>auth.js</code></p>
  <pre><code>import firebase from &quot;firebase/app&quot;;
  import &quot;firebase/auth&quot;;
  
  export default function ({store, router}) {
    firebase.auth().onAuthStateChanged((user) =&gt; {
        if(user){
          store.dispatch('Authentication/SET_USER',JSON.stringify(user))
        }else{
          store.dispatch('Authentication/SET_USER', null)
        }
    })
  }
  </code></pre>
  <p>Vuex: <code>Authentication.js</code></p>
  <pre><code>export const state = () =&gt; ({
      user: ''
  });
  
  export const getters = {
      user: state =&gt; {
        return state.user;
      },
    
    }
  
  export const mutations = {
      SET_USER: (state, payload) =&gt; {
          state.user = payload;
      },
  }
  
  export const actions = {
      SET_USER({ commit }, payload) {
          commit('SET_USER', payload)
      },
  }
  </code></pre>