Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Yasser Elgammal
Yasser Elgammal

Posted on

     

Dive into Laravel Sanctum Token Abilities

Laravel has great feature including in Sanctum, that allow to identify logged user and handle the authorize process through token,

This feature is called Sanctum Token Abilities

By using Sanctum Token Abilities you can With Laravel sanctum abilities you can specify ability once you create token,

You can use this ability to manage roles and routes due to specific abilities,

For example, you can use Sanctum Token Abilities to set routes that are accessible only to users with specific abilities. You can also perform actions based on a user's abilities, such as showing or hiding certain features or data.

It's simple and you can use it if you don't have multiple or complex roles

Let's practise with example:

1- Add following Middlewares lines to$middlewareAliases insideApp\Http\Kernel

'abilities'=>\Laravel\Sanctum\Http\Middleware\CheckAbilities::class,'ability'=>\Laravel\Sanctum\Http\Middleware\CheckForAnyAbility::class,
Enter fullscreen modeExit fullscreen mode

2- Assign ability to user with poweful sanctum token by usign this:

$user->createToken('token-name',['admin'])->plainTextToken;
Enter fullscreen modeExit fullscreen mode

3- Now we can protect our routes, by specifying abilites

In this example, we're protecting the /admin/index route so that only users with the [moderator & admin] ability can access it.

Route::middleware(['auth:sanctum','abilities:moderator,admin'])->prefix('admin')->group(function(){Route::get('index',[AdminController::class,'index']);});
Enter fullscreen modeExit fullscreen mode

In this example, we're protecting the /admin/index route so that only users with the [moderator or admin] ability can access it, it means or user has at least one with access the route.

Route::middleware(['auth:sanctum','ability:moderator,admin'])->prefix('admin')->group(function(){Route::get('index',[AdminController::class,'index']);});
Enter fullscreen modeExit fullscreen mode

Also, we can check if user token has a specific ability for example through our controller,

if(auth()->user()->tokenCan('admin')){dd('Hello Admin');}
Enter fullscreen modeExit fullscreen mode

Summary:
Sanctum Token Abilities is a feature of the Laravel Sanctum package that allows you to specify abilities for a token when it's created. These abilities can be used to manage roles and restrict access to certain parts of your Application.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software engineer @ AAIT
  • Location
    Egypt, Damietta
  • Work
    Software Engineer | Backend Developer | PHP | Laravel | Fintech
  • Joined

More fromYasser Elgammal

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp