Skip to main content

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? (36 h 00 min)

You also get:

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

Already a member? Login here

Md Mahmudul Hasan avatar

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

Povilas Korop avatar

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

Rangoh avatar

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

DOOMitru avatar

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

Nerijus avatar

it should :D

👍 1
Tariq avatar

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

Nerijus avatar

In short build assets and deploy as you would regularly

Tariq avatar

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.

Nerijus avatar

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

Dmytro Sakharuk avatar

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 }
}
jwinder avatar

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

jwinder avatar

This was a great lesson.

zaini zain avatar

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

Nerijus avatar

What do you mean?

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.