Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Permissions Front-end: Vue CASL & v-if

Premium
6 min read

Now that we have authorization for the back-end, let's add it for the front-end, too. If the user cannot delete the post, then he shouldn't even see the delete button.

delete action doesn't show


For front-end permissions, we will use the package CASL Vue. First, let's install it.

npm install @casl/vue @casl/ability

To get started, we need to import abilitiesPlugin and ability from the services in the main app.js.

resources/js/app.js:

import './bootstrap';
 
import { createApp, onMounted } from 'vue'
 
import router from './routes/index'
import VueSweetalert2 from 'vue-sweetalert2';
import useAuth from './composables/auth';
import { abilitiesPlugin } from '@casl/vue';
import ability from './services/ability';
 
createApp({
setup() {
const { getUser } = useAuth()
onMounted(getUser)
}
})
.use(router)
.use(VueSweetalert2)
.use(abilitiesPlugin, ability)
.mount('#app')

And what is inside this /services/ability? You define the abilities there, and one of the sections in the documentation is about ability builder. And we can copy the code in that services file.

resources/js/services/ability.js:

import { AbilityBuilder, Ability } from '@casl/ability'
 
const { can, cannot, build } = new AbilityBuilder(Ability);
 
export default build();

But instead of defining can and cannot here, we will define them based on...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

MM
Md Mahmudul Hasan ✓ Link copied!

Hi Laravel daily, We got vue, react with laravel... Great effort by you. It will be really great if you have something related to typescript now. Thank you in advance

PK
Povilas Korop ✓ Link copied!

Sorry, not in plans, as our team doesn't actively work with typescript.

R
Rangoh ✓ Link copied!

Now, I am able to understand how Vue.js+Laravel of QuickAdmin panel has been build. Really helpful.

D
DOOMitru ✓ Link copied!

"Ant what is inside..." should probably be "And what is inside..."

N
Nerijus ✓ Link copied!

it should :D

T
Tariq ✓ Link copied!

great course, now can you help me about how I can make it ready for server/deployment?

N
Nerijus ✓ Link copied!

In short build assets and deploy as you would regularly

T
Tariq ✓ Link copied!

I build the assests using "npm run build", now when i deploy on server i get wrning messages in console

Loading failed for the module with source "http://[::1]:5174/resources/js/app.js" Loading failed for the module with source “http://[::1]:5174/@vite/client”

and page keeps loading, nothing shows up. I know its not in the scope of the course, but if you could have written a lesson on how to deploy, then it would have been really helpful for newbies like me.

N
Nerijus ✓ Link copied!

I guess you build assets locally which are git ignored by default. We have a tutorial how to build assets and deploy them. If you really need help come to discord where it will be easier to

DS
Dmytro Sakharuk ✓ Link copied!

I honestly don't understand why we need to complicate things, make another request, and install additional packages?

const user = reactive({
	name: '',
	email: '',
	abilities: []
})

export default function useAuth() {

	//...

	const loginUser = (response) => {
		user.name = response.data.name
		user.email = response.data.email
		user.abilities = response.data.abilities

		localStorage.setItem('loggedIn', JSON.stringify(true))
		router.push({ name: 'posts.index' })
	}

	//...

	const can = (ability) => {
		return user.abilities[ability] ?? false;
	}

	return { loginForm, validationErrors, processing, submitLogin, user, getUser, logout, can }
}
J
jwinder ✓ Link copied!

Thats a great point. Even if more permissions to check were added later that solution would still work.

J
jwinder ✓ Link copied!

This was a great lesson.

ZZ
zaini zain ✓ Link copied!

can anyone help on how to prevent direct url? and also example for menu navigation tab like using canany

N
Nerijus ✓ Link copied!

What do you mean?