Overview
C.A.R. 911 uses Laravel Sanctum for API authentication, providing a lightweight authentication system for SPAs (Single Page Applications) and mobile applications. Sanctum offers both session-based authentication for first-party applications and token-based authentication for third-party API access.Laravel Sanctum is installed via the
laravel/sanctum package (version 2.11+) as specified in composer.json.Authentication Methods
Session-Based Authentication (Web)
For web-based applications, C.A.R. 911 uses traditional session-based authentication with cookies. This is the primary method for the main web interface. Login Endpoint:/login (POST)
Logout Endpoint: /logout (POST)
Token-Based Authentication (API)
For API access, Sanctum provides personal access tokens that can be included in request headers.User Model Configuration
The User model includes theHasApiTokens trait, enabling token-based authentication:
app/Models/User.php
Authentication Endpoints
Login
Authenticate a user and create a session:| Parameter | Type | Required | Description |
|---|---|---|---|
email | string | Yes | User’s email address |
password | string | Yes | User’s password (minimum 8 characters) |
Logout
Terminate the current user session:Get Authenticated User
Retrieve information about the currently authenticated user:Creating API Tokens
To generate personal access tokens for API authentication, you can use Sanctum’s token creation method:Using API Tokens
Include the token in theAuthorization header of your requests:
Example Authenticated Request
Sanctum Configuration
Stateful Domains
Sanctum allows requests from specific domains to receive stateful API authentication cookies:config/sanctum.php
Token Expiration
By default, Sanctum tokens do not expire:config/sanctum.php
Middleware Protection
Protect your API routes using theauth:sanctum middleware:
routes/api.php
Authorization with Spatie Permissions
C.A.R. 911 uses Spatie Laravel Permission package for role-based access control. Users are assigned roles, and roles have specific permissions.User Model with Roles
Checking Permissions
You can check if a user has specific permissions:Route Protection with Permissions
Many routes in C.A.R. 911 are protected with middleware checking for specific permissions:routes/web.php
Refer to the Roles and Permissions documentation for detailed information about available permissions.
Error Responses
Unauthenticated
When authentication is required but not provided: Status Code:401 Unauthorized
Invalid Credentials
When login credentials are incorrect: Status Code:422 Unprocessable Entity
Insufficient Permissions
When the user lacks required permissions: Status Code:403 Forbidden
Security Best Practices
Recommendations
- Store tokens securely - Never expose API tokens in client-side code or version control
- Use HTTPS - Always use secure connections in production
- Rotate tokens regularly - Implement token rotation policies for enhanced security
- Validate input - All authentication endpoints validate input as defined in
RegisterController:- Email must be valid and unique
- Password must be at least 8 characters
- Name is required
- Session timeouts - Configure appropriate session lifetimes in
.env: - Password hashing - Passwords are automatically hashed using Laravel’s
Hash::make()method
Guest Routes
Certain routes are accessible only to unauthenticated users (guests):app/Http/Controllers/Auth/LoginController.php
Next Steps
User Management
Learn how to manage users, profiles, and themes
Roles & Permissions
Understand role-based access control with Spatie