In a laravel + vuejs project, I am trying to access my currentUser role in my Acces.js file using store.getters.currentUser but encountering an error:
Error in render: "TypeError: Cannot read property 'getters' of undefined
I have tried multiple solutions without success. Can anyone provide assistance?
Here is my Acces.js:
import {store} from './store'
export default class Acces {
Admin(){
return store.getters.currentUser.role ==="admin";
}
NotUser(){
return store.getters.currentUser.role === "admin" || store.getters.currentUser.role === "chef de projet" ||store.getters.currentUser.role === "client" ;
}
Chef(){
return store.getters.currentUser.role === "chef de projet";
}
client(){
return store.getters.currentUser.role === "client";
}
adminchef(){
return store.getters.currentUser.role === "admin" || store.getters.currentUser.role === "chef de projet";
}
chefUser(){
return store.getters.currentUser.role !== "admin" || store.getters.currentUser.role === "chef de projet" ||user.role !== "client"
}
};
And here is my store.js:
import { getLocalUser } from "./helpers/auth";
const user = getLocalUser();
export default {
state: {
currentUser: user,
isLoggedIn: !!user,
loading: false,
auth_error: null,
customers: []
},
getters: {
isLoading(state) {
return state.loading;
},
isLoggedIn(state) {
return state.isLoggedIn;
},
currentUser(state) {
return state.currentUser;
},
authError(state) {
return state.auth_error;
},
customers(state) {
return state.customers;
}
},
mutations: {
login(state) {
state.loading = true;
state.auth_error = null;
},
loginSuccess(state, payload) {
state.auth_error = null;
state.isLoggedIn = true;
state.loading = false;
state.currentUser = Object.assign({}, payload.user, {token: payload.access_token});
localStorage.setItem("user", JSON.stringify(state.currentUser));
},
loginFailed(state, payload) {
state.loading = false;
state.auth_error = payload.error;
},
logout(state) {
localStorage.removeItem("user");
state.isLoggedIn = false;
state.currentUser = null;
},
updateCustomers(state, payload) {
state.customers = payload;
}
},
actions: {
login(context) {
context.commit("login");
},
getCustomers(context) {
axios.get('/api/customers')
.then((response) => {
context.commit('updateCustomers', response.data.customers);
})
}
}
};