My index.js
file holds my routes, and I'm attempting to establish a beforeEnter
guard for the admin panel route to only permit authenticated admins. However, when I check
console.log(store.getters.isLoggedIn)
, the output is:
ƒ isLoggedIn(state) {
return state.user.token ? true : false
}
rather than true
or false
. I'm uncertain as to why this is occurring. My getter function is defined as:
getters: {
isLoggedIn: state => {
return state.user.token ? true : false
}
}
and here are my routes:
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import AdminPanel from '../views/AdminPanel.vue'
import store from "../store/authentication.js";
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: Home
},
{
path: "/admin",
component: AdminPanel,
beforeEnter: (to, from, next) => {
console.log(store.getters.isLoggedIn)
}
}
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
The contents of my store.js file:
import Vue from 'vue'
import Vuex from 'vuex'
import authentication from './authentication'
import cart from './cart'
Vue.use(Vuex)
const store = new Vuex.Store({
modules: {
authentication,
cart
}
})
export default store
In authentication.js:
const authentication = {
state: {
user: null
},
getters: {
isLoggedIn: state => {
return state.user.token ? true : false
}
}
}
export default authentication