1. Choosing Between Session & Token Auth
Session-based auth (Sanctum cookies) is ideal for same-origin SPAs. Stateless JWT is preferred for mobile apps or cross-domain API consumers. Evaluate the threat model before choosing.
2. Laravel Sanctum for SPA Authentication
Sanctum issues CSRF-protected session cookies for first-party SPAs. Configure `SANCTUM_STATEFUL_DOMAINS` to match your frontend's origin. Never skip CSRF middleware for POST/PUT/DELETE routes.
3. JWT Tokens for API Consumers
Use `tymon/jwt-auth` for stateless token authentication. Store tokens in HTTP-only cookies on web clients, never in `localStorage`, to prevent XSS token theft.
4. Role-Based Access Control (RBAC)
Implement roles and permissions using a pivot table (`role_user`, `permission_role`) pattern or Spatie's `laravel-permission` package. Gate checks with `$this->authorize('action', Model::class)` in controllers.
5. Policy Classes & Resource Authorization
Create model-specific Policy classes and bind them in `AuthServiceProvider`. Policies centralize authorization logic and keep it out of controllers.
6. Protecting Against Common Auth Attacks
Rate limit login endpoints, enforce bcrypt cost factor ≥ 12, validate password reset tokens with time-limited signed URLs, and rotate sanctum tokens after privilege escalation.