[Vuejs]-Write a Global Methods to check authentication in NuxtJS

0👍

Since authUser is a state property in vuex, not a method. LoggedIn in your component is simply returning a value from the state and does not need to be a method.

You should use a computed instead of a method. You also do not need to call LoggedIn from the created method, once it is a computed, it will be calculated automatically.

<script>
import '~/assets/icons'
export default {
  head () {
    return !this.mobileLayout ? {} : {
      bodyAttrs: {
        class: 'mobile'
      }
    }
  },
  computed: {
    LoggedIn: function () {
      return this.$store.state.authUser
    }
  }
}
</script>

Or even better, use mapState from vuex which is documented here https://vuex.vuejs.org/en/state.html

<script>
import Vuex from 'vuex'
import '~/assets/icons'
export default {
  head () {
    return !this.mobileLayout ? {} : {
      bodyAttrs: {
        class: 'mobile'
      }
    }
  },
  computed: {
    ...mapState({
        LoggedIn: 'authUser'
    })
  }
}
</script>

Your template does not need to be changed.

Leave a comment