Laravel Sanctum for API authentication

4746
0

Laravel Sanctum provides lightweight API authentication for SPAs and mobile apps. For SPAs on the same domain, Sanctum uses Laravel's session cookies with CSRF protection. For mobile apps or third-party clients, it issues API tokens stored in a personal_access_tokens table. I protect API routes with the auth:sanctum middleware. Users authenticate via login endpoints that set cookies or return tokens. Token abilities scope permissions—read-only vs full access. The currentAccessToken() method retrieves the token for ability checks. Sanctum is simpler than OAuth for first-party applications while still secure. It handles token expiration, revocation, and multiple tokens per user for different devices.