ability update is working only after Refreshing the page in VueJS
See original GitHub issueI used ability in router (vue-router) like this in router/index.js
{
path: 'warehouses',
name: 'Warehouses',
component: Warehouse,
meta: {
hasPerm: ability.can('list', 'warehouse-balances')
}
},
and updating ability when loginSuccess
mutation is executed
in store/ability.js
export const ability = new Ability()
export const abilityPlugin = (store) => {
ability.update(store.state.rules);
return store.subscribe((mutation) => {
switch (mutation.type) {
case 'loginSuccess':
let formattedRules = [];
if(mutation.payload.permissions.length > 0) {
formattedRules = mutation.payload.permissions.map(perm => {
let formattedObj = {};
formattedObj.actions = perm.substr(0, perm.indexOf(' '));
formattedObj.subject = perm.substr(perm.indexOf(' ')+1);
return formattedObj;
})
}
ability.update(formattedRules);
break
case 'logoutSuccess':
ability.update([{ actions: 'read', subject: 'all' }])
break
}
})
}
But ability is updating only when page is refreshed
I am checking the route meta in beforeEach
guard it is returning false but it should be true after login
router/index.js
router.beforeEach((to, from, next) => {
const isPublic = to.matched.some(record => record.meta.public);
// CHECK IF USER HAS PERMISSION FOR THIS URL
const isOpen = ('hasPerm' in to.meta) ? !!to.meta.hasPerm : true;
//ability.update(store.state.rules);
const onlyWhenLoggedOut = to.matched.some(record => record.meta.onlyWhenLoggedOut);
const loggedIn = !!TokenService.getToken();
if (!isPublic && !loggedIn) {
return next({
path: '/pages/login',
query: {redirect: to.fullPath}
})
}
if (!isOpen) {
return next('/');
}
if (loggedIn && onlyWhenLoggedOut) {
return next('/');
}
next();
});
I don’t know if it is a bug or I am doing something wrong. Any help would be appreciated ! This issue is also published in Forum vuejs here is the link Question about this issue
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
vue.js - casl - can() returns null after reloading (F5) page
I'm facing the same issue with a Vue 2 project - everytime i refresh the page the $ability instance gets reseted to its...
Read more >Plugin For Vuex Only Working After Refresh - Vue Forum
the store.state.rules is the data I am injecting into the new ability instance however like I said it only happens after refreshing the...
Read more >How to detect page is refreshed on vue? How can Vue router
Javascript answers related to “vue detect page reload” nuxt reload page; how to use the onload event n vue js; re init data...
Read more >vue detect page refresh - Retro Remix Advance
“detect page refresh and redirect vuejs” Code Answer vue router refresh ... It'll only recreate the component if the key value matches what...
Read more >Handling Service Worker updates in your Vue PWA
Vue.js is awesome. ... When you install the plugin-pwa it adds a ... Now we just need to reload the page once the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is the way things work 😃
If you do assignment to a variable it will never be changed until you change the variable. You just cached results of the ability, into object key. Vue router is initialized once and its routes are also initialized once and are not re-evaluated in
beforeEach
.So, your example is the same as:
aah got it! Thanks