laravel/sanctum
Laravel Sanctum is a lightweight authentication package for Laravel, ideal for SPAs and simple APIs. It supports cookie-based session auth for first-party SPAs and API tokens for personal access tokens, with minimal configuration and Laravel-first integration.
laravel/sanctum) and a single php artisan vendor:publish command suffices. Migration and model setup are automated.auth:sanctum) for seamless API protection, reducing custom logic.getAccessTokenFromRequestUsing).personal_access_tokens table).sanctum.stateful config).personal_access_tokens table. Pre-existing apps must handle migrations or schema conflicts (though Sanctum provides them).tokenable_id, name, and abilities (added in v4.2.0) mitigate lookup overhead but may need tuning for large-scale deployments.SameSite cookie attributes to be configured correctly.localStorage) is compromised (mitigated by short-lived tokens/expiration).MissingScopeException) are deprecated; review changelog for breaking changes in future upgrades.expires_at but requires manual cleanup (e.g., via Laravel Queues).personal_access_tokens table).SameSite, Secure flags)?fetch/axios with cookies or tokens).Authorization: Bearer headers.composer require laravel/sanctum
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
php artisan migrate
config/sanctum.php (e.g., stateful domains, token TTL).HasApiTokens trait.auth:sanctum middleware.tokenCant() or tokenCan() for access control.axios with credentials) or token storage.access_token, token_type).Authorization headers or cookies.last_used_at).config/sanctum.php (e.g., token TTL, stateful domains).personal_access_tokens table (contains sensitive data).php artisan tinker to inspect tokens; Sanctum::resolveToken() for custom token retrieval.tokenable_id and name improve lookup. For high volume, consider:
TokenRepository).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Database downt |
How can I help you explore Laravel packages today?