title: Access LocalStorage in Middleware - NuxtJs
body: <p>Well, I'm starting with nuxt and I have following routes:</p>
  
  <pre><code>/home
  
  /dashboard
  
  /login
  </code></pre>
  
  <p>I want to protect the /dashboard, but only for users logged in with a token in <code>localStorage</code>.</p>
  
  <p>The simplest way I thought of doing this was by creating a /middleware/auth.js</p>
  
  <pre><code>export default function () {
    if (!window.localStorage.getItem('token')) {
      window.location = '/login'
    }
  }
  </code></pre>
  
  <p>and registering it in the /dashboard/index.vue component.</p>
  
  <pre><code>&lt;script&gt;
  export default {
    middleware: 'auth',
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>But I cannot access <code>localStorage</code> within a middleware, because LocalStorage is client-side.</p>
  
  <p>I have already tried to add this same check in the <code>created()</code> dashboard layout, but I cannot return window not set <code>mounted()</code> is too late, it can only check after the page has been fully assembled.</p>
  
  <p>So how can I achieve this?
  Note: I do not intend to use any Vuex for this project.</p>