laravel/sanctum
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
illuminate/console | v10.48.10 | - | MIT | prod | |
illuminate/contracts | v10.48.10 | - | MIT | prod | |
illuminate/database | v10.48.10 | - | MIT | prod | |
illuminate/support | v10.48.10 | - | MIT | prod |
Laravel Sanctum is a powerful and lightweight library that offers an effective authentication system for Single Page Applications (SPAs) and simple APIs. Laravel Sanctum is designed to provide a simple and enjoyable development experience for both beginners and experienced PHP developers. A notable feature of Laravel Sanctum is its ability to offer robust mechanisms for issuing API tokens to your application's users. It also ensures a secure SPA authentication, keeping your customer data safe and your applications running smoothly.
Using Laravel Sanctum involves a number of easy-to-follow steps. First, install the Laravel Sanctum via Composer:
composer require laravel/sanctum
Once installed, you must publish the Sanctum configuration and migration files using the vendor:publish
Artisan command:
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
Next, you need to run your database migrations using:
php artisan migrate
In your app/Http/Kernel.php
file, you should add Laravel Sanctum’s middleware:
use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;
'api' => [
EnsureFrontendRequestsAreStateful::class,
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
To use Sanctum's tokenCan
method to authenticate certain actions, you need to add Laravel\Sanctum\HasApiTokens trait to your App\Models\User model:
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
}
Now, you are ready to start using Laravel Sanctum. Let's login a user, and provide a token:
$user = User::find(1);
$token = $user->createToken('my-token')->plainTextToken;
return response()->json(['token' => $token]);
In your client side, simply use this token in the Authorization header:
Authorization: Bearer my-token
For a comprehensive walkthrough of Laravel Sanctum and further code examples, kindly refer to the official documentation which can be found on the Laravel website at https://laravel.com/docs/sanctum. In the documentation, you'll find additional details that cover a wider scope of the usage and integration possibilities of Laravel Sanctum.