Static GitHub Issues

[185] read local storage on default layout?

prev: connection resources
next: Fetch data from an api for all views at first load

Is there any way to deal with localStorage, in order to set a property based on it for the entire application?

Let's say I want to read a locally stored token to determine if a user has access to an API, and then set a boolean property on the layout, to be propagated to the relevant components, ie. navbar, login, logout, etc.

something like this:

<template>
  <div>
    <app-nav :authed="authed"/>
    <nuxt :authed="authed"/>
  </div>
</template>

<script>
  import AppNav from '~components/Nav.vue'

  export default {
    data: function () {
      return {
        authed: false
      }
    },

  components: {
    AppNav
  },

  created: () => {
    this.authed = localStorage.getItem('token') !== null
  }
}
</script>

This example would generate an error localStorage is not defined

How can I accomplish this?

<!--cmty--><!--cmty_prevent_hook--><div align="right"><sub><em>This question is available on <a href="https://nuxtjs.cmty.io">Nuxt.js</a> community (<a href="https://nuxtjs.cmty.io/nuxt/nuxt.js/issues/c157">#c157</a>)</em></sub></div>