Integration with spatie-laravel-permissions #420
-
Hello) I am developing an application with two separate servers, api + ui. A question arose. How can I correctly use the laravel-permission library to do v-can type checks for rendering ui elements? Something like the https://github.com/dystcz/nuxt-permissions library, only with importing permissions from the laravel-permissions library. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
Hey @projct1, I've never worked with @can('edit articles')
// Code visible only for users with permissions
@endcan If you can provide some code sample, that would be nice as well |
Beta Was this translation helpful? Give feedback.
-
Here is the best example https://youtu.be/r3JKDe6V8xk?si=pr-yfnCRQGLk-K5J&t=122 |
Beta Was this translation helpful? Give feedback.
-
Now I see something like this:
Route::get('user', function(Request $request) {
$user = $request->user();
$user->can = $user->getPermissionsViaRoles()->flatMap(
fn($permission) => [ $permission->name => $user->can($permission->name) ]
);
return $user;
})->middleware('auth:sanctum');
|
Beta Was this translation helpful? Give feedback.
-
@manchenkoff, by the way, this is a bit off topic, but could you please give me a hint? class PlacePolicy
{
public function edit(User $user, Place $place): bool
{
return true;
}
}
class PlaceController extends Controller
{
public function edit(Place $place): Place
{
return $place;
}
}
//api.php
Route::get('places/{place}', [PlaceController::class, 'edit'])->middleware('auth:sanctum', 'can:edit,place'); I get a 403 access error. P. S. I using nuxt-auth-sanctum library and getting requests from ui server. |
Beta Was this translation helpful? Give feedback.
Okay, I see, thanks for the reference. If you go this way, you can just use
useSanctumUser
composable to extract permissions viauser.can
property. I do not see any benefit of havingusePermissions
on the module level, especially since this functionality is not a part of Sanctum, but some external package which people might not use at all.I would suggest implementing this helper composable as a part of your application, as you mentioned. However, keep in mind, that user identity is not refetched while you are navigating between pages, so if some permissions have been revoked or assigned, you have to refetch identity manually via
refreshIdentity
method available as a part ofuseSanctumAuth
…