This is a example for Laravel implementation. This requires Laravel Permissions installed and configured.
The frontend needs to know the permissions assigned, for this purpose one endpoint are created that return this information.
In this case we will use the magic method
__invoke
to send the function call in the routes
/**
* Display a listing of permissions from current logged user.
*
* @return \Illuminate\Http\JsonResponse
*/
public function __invoke()
{
return auth()->user()->getAllPermissions()->pluck('name');
}
The frontend needs to know the roles assigned, for this purpose one endpoint are created that return this information.
In this case we will use the magic method
__invoke
to send the function call in the routes
/**
* Display a listing of roles from current logged user.
*
* @return \Illuminate\Http\JsonResponse
*/
public function __invoke()
{
return auth()->user()->getRoleNames();
}
These two routes should only be accessed when you are authenticated.
Route::namespace('Auth')->group(function () {
Route::get('permissions', 'PermissionController');
Route::get('roles', 'RoleController');
});
The plugin creates an instance of Laravel accessible globally, and at the same time it registers all the directives.
import Vue from 'vue';
import VueGates from 'vue-gates';
Vue.use(VueGates);
const { data: roles } = await axios.get('/api/roles');
const { data: permissions } = await axios.get('/api/permissions');
this.$gates.setRoles(roles);
this.$gates.setPermissions(permissions);
That's all. Now you can start using the directives.