question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ability update is working only after Refreshing the page in VueJS

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stalniycommented, Mar 28, 2019

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:

let isAllowed = false
const can = () => isAllowed

const route = {
  meta: {
    hasPerm: can()
  }
}

isAllowed = true

console.log(can(), route.meta.hasPerm)
0reactions
bakhti-uzbcommented, Mar 28, 2019

aah got it! Thanks

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found