laravel/airlock
Laravel Sanctum (formerly Airlock) offers lightweight authentication for Laravel SPAs and simple APIs. Use cookie-based session auth for first-party SPAs or issue API tokens for mobile apps and third-party clients, with minimal setup and seamless Laravel integration.
personal_access_tokens table) instead of sessions, reducing server-side storage overhead and enabling seamless cross-domain/authentication flows.Auth::user(), HasApiTokens trait) and middleware (auth:sanctum).laravel/sanctum) + php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" + migrations (php artisan migrate).personal_access_tokens table (or uses existing oauth_personal_access_tokens in Laravel 10+). No complex schema changes.auth:sanctum middleware and /sanctum/csrf-cookie, /sanctum/token endpoints. Can be customized via config (config/sanctum.php).createToken() methods on models).getAccessTokenFromRequestUsing).| Risk Area | Assessment | Mitigation |
|---|---|---|
| Laravel Version Lock | Tight coupling with Laravel (v11+ in v4.x). Downgrading/upgrading Laravel may require Sanctum updates. | Monitor Laravel/Sanctum compatibility matrix. Use feature flags or modularize auth if version flexibility is critical. |
| Token Security | Tokens are not encrypted by default (stored as plaintext in DB). Vulnerable to DB leaks if not using HTTPS or additional encryption (e.g., Laravel Encryption). | Enable stateful mode for CSRF protection, use HTTPS, and consider encrypting tokens at rest (e.g., via Laravel’s encrypt()). |
| Performance | Token lookup uses where('tokenable_id', $id)->where('token', $token) by default. Scaling to millions of tokens may require indexing (already added in v4.2.0). |
Ensure tokenable_id and token columns are indexed. For high-scale APIs, consider sharding tokens or using a dedicated cache (Redis) for token validation. |
| Stateful Mode Complexity | Stateful mode (cookies) introduces CSRF and session-like behavior, which may conflict with stateless APIs or CDNs. | Use stateless mode for APIs; reserve stateful for SPAs. Configure stateful domains explicitly in config/sanctum.php. |
| Token Revocation | Manual revocation requires deleting tokens from DB. No built-in TTL for stateless tokens (unlike OAuth). | Implement a token_pruner job (via Laravel Queues) to expire tokens after inactivity (e.g., last_used_at tracking in v4.3.0). |
| Custom User Providers | Sanctum assumes Laravel’s default User model. Custom providers (e.g., API keys) require manual guard configuration. |
Extend Sanctum::guard() or create a custom guard implementation. |
Illuminate\Contracts\Auth\Authenticatable).auth:sanctum).HasApiTokens trait).getAccessTokenFromRequestUsing).| Step | Action | Dependencies |
|---|---|---|
| 1. Assessment | Audit existing auth (e.g., sessions, API keys, Passport). Identify endpoints/models needing Sanctum. | None. |
| 2. Setup | Install Sanctum: composer require laravel/sanctum. Publish config/migrations: php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider". Run migrations. |
Laravel 11+, PHP 8.4+. |
| 3. Model Integration | Add HasApiTokens trait to user models. Configure token namespaces (e.g., createToken('mobile-app')). |
Eloquent models. |
| 4. Route Protection | Apply auth:sanctum middleware to API routes. For SPAs, enable stateful mode in config/sanctum.php. |
Laravel routing. |
| 5. Frontend Setup | Configure frontend to send tokens (stateless: Authorization: Bearer <token>; stateful: cookies). |
SPA framework (e.g., Axios for React). |
| 6. Testing | Test token generation, revocation, and scope-based access. Validate CSRF protection for stateful requests. | Postman/Newman or custom test suites. |
| 7. Monitoring | Set up logging for token usage (e.g., last_used_at). Monitor failed auth attempts. |
Laravel Horizon or custom logs. |
| 8. Rollback Plan | Document steps to revert to legacy auth if issues arise (e.g., token migration failures). | Backup DB schema. |
oauth_personal_access_tokens.api middleware group. Can coexist with Passport if needed.getAccessTokenFromRequestUsing.How can I help you explore Laravel packages today?