Where is need to set roles and permissions?
s-fog opened this issue · comments
I try to set it in store, but this.$laravel is not exists there. Where i need to do it
this.$laravel.setPermissions(this.$store.state.permissions); this.$laravel.setRoles(this.$store.state.roles);
I can set it in some component when 'mounted' or 'created' triggered, but it is no sense, cause it is already rendered.
Could someone help me, please?
If setting this in app.js, error is coming that 'Cannot read property '$laravel' of undefined' I cant find place when i can set it. Help please
Please help somebody
@s-fog You could do it from the created
method of a layout, or from Vuex when you get the user's profile.
Actually I do have the same problem when trying to access this.$gates
in my Vuex store.
As this
refers to the store, I do get the following error: Uncaught (in promise) TypeError: Cannot read property '$gates' of undefined
I try to call it in my store after logging in (and setting the user data)
axios.get('/api/roles').then(response => { commit('SET_ROLES', response.data) this.$gates.setRoles(response.data) })
@denyo256 this
cannot be used from Vuex, but there are two ways to access $gates
from Vuex:
Nuxt.js:
actions: {
yourAction ({ commit }, { req, $gates }) {
const user = req.session.user
if (user) {
$gates.setRoles(user.roles)
$gates.setPermissions(user.permissions)
commit('user', user)
}
}
}
https://williamcruzme.github.io/vue-gates/#/usage/ssr?id=the-nuxtserverinit-action
Vue.js
import Vue from 'vue';
export const actions = {
yourAction() {
Vue.prototype.$gates.setRoles(['admin']);
},
};