Configuration
Sanctum configuration is defined inconfig/sanctum.php.
Stateful Domains
Domains that receive stateful API authentication cookies:Authentication Guards
Sanctum checks theweb guard for authentication:
Token Expiration
Tokens expire after 1 year:null for tokens that never expire.
Generating Tokens
TheUser model uses the HasApiTokens trait from Laravel Sanctum (app/Models/Auth/User.php:69):
Via Filament Panel
Currently, the primary method to generate tokens is through the Filament admin panel:- Log in to the admin panel at
/admin - Navigate to your user profile
- Generate a new personal access token
- Copy and securely store the token
Programmatically (for reference)
Tokens can be created programmatically using:Using Tokens
Authorization Header
Include the token in theAuthorization header using the Bearer scheme:
cURL Example
JavaScript/Fetch Example
PHP/Guzzle Example
Python/Requests Example
Middleware
The Sanctum authentication middleware is applied to protected routes.Authenticate Middleware
Example fromMyController (app/Http/Controllers/Api/Auth/User/Me/MyController.php:25):
Protected Routes
User Endpoints
The following endpoints require authentication: Current UserToken Management
Token Model
Tokens are stored using Laravel Sanctum’sPersonalAccessToken model.
The User model defines the relationship (app/Models/Auth/User.php:59):
Revoking Tokens
Tokens can be revoked programmatically:Security Best Practices
Token Storage
- Never commit tokens to version control
- Store securely in environment variables or secure vaults
- Use HTTPS only for API requests with tokens
- Rotate tokens regularly for enhanced security
Token Scope
While Sanctum supports token abilities/scopes, AnimeThemes Server currently relies on role-based permissions instead.Rate Limiting
Authenticated requests may have different rate limits than anonymous requests. Users with thebypass api rate limiter permission are exempt from rate limiting.
Troubleshooting
401 Unauthorized
- Verify token is included in
Authorizationheader - Ensure token format is
Bearer TOKEN - Check token hasn’t been revoked
- Verify token hasn’t expired
403 Forbidden
- User is authenticated but lacks required permissions
- See Permissions documentation
Invalid Token Format
Sanctum tokens have the format:{token_id}|{plain_text_token}
Example: 1|abcdefghijklmnopqrstuvwxyz1234567890
Next Steps
- Authentication Overview - General authentication concepts
- Permissions - Understanding role-based access control