[Vuejs]-Ensure variable exist in store state using vuex

0๐Ÿ‘

โœ…

If you using the vue router you can authenticate a user there:

const ifAuthenticated = (to, from, next) => {
if (store.state.token) {  // or state.user etc.
  next()
  return
}
next('/Adminlogin')  // if not authenticated

and the router path looks like that:

{
  path: '/AdminUI',
  name: 'AdminUI',
  component: AdminUI,
  beforeEnter: ifAuthenticated

}

Another possible solution:

<v-template
    v-show="$store.state.isUserLoggedIn"
</v-template>

dont forget to import { mapState } from "vuex";

and in the store:

getters: {
  isUserLoggedIn: state => !!state.token
}

Leave a comment