Vue.js with Laravel Permission

后端 未结 5 631
旧时难觅i
旧时难觅i 2020-12-31 22:38

I am in the process of integrating Laravel Permission API with Vue.JS frontend. I am using https://github.com/spatie/laravel-permission library for Laravel Permission. I am

5条回答
  •  天命终不由人
    2020-12-31 22:43

    I will do a ajax call to check for permissions instead , something like this, but of cours eyou need to modify it to cater your needs.

    Routes:

    Route::get('/permission/{permissionName}', 'PermissionController@check');
    

    Controller:

    function check($permissionName) {
       if (! Auth::user()->hasPermissionTo($permissionName)) {
            abort(403);
       }
       return response('', 204);
    }
    

    Vue: (if you wanna do this synchronously), this is a simple example (Vue global mixin), you can turn this into Vue directive or component.

    Vue.mixin("can", (permissionName) => {
        let hasAccess;
        axios.get(`/permission/${permissionName}`)
            .then(()=> {
                hasAccess = true;
            }
            .catch(()=> {
                hasAccess = false;
            };
        return hasAccess;
    });
    
    

    And then everytime you wanna check permission, you can just do

     
    

提交回复
热议问题