Introduction
Backend App uses Laravel Sanctum for API token authentication. Sanctum provides a lightweight authentication system for SPAs (single page applications), mobile applications, and simple, token-based APIs.Why Laravel Sanctum?
Laravel Sanctum is included in this project (version 4.0) as specified incomposer.json:
composer.json
- Simple token-based authentication for API requests
- No OAuth complexity - just straightforward API tokens
- Built-in token management - create, revoke, and manage tokens easily
- Seamless Laravel integration - works with existing authentication guards
How Authentication Works
User Creates Account
Users register through your application and receive credentials (email/password).
Include Token in Requests
The client includes the token in the
Authorization header for all API requests.The Authenticated User Endpoint
The API includes a protected endpoint that returns the currently authenticated user:routes/api.php
- Requires the
auth:sanctummiddleware - Returns the authenticated user’s data
- Responds with 401 Unauthorized if no valid token is provided
Example Request
Example Response
Notice that sensitive fields like
password and remember_token are automatically hidden from the response. This is configured in the User model’s $hidden property.Authentication Configuration
Authentication is configured inconfig/auth.php:
config/auth.php
User Model
TheUser model includes the necessary traits and configuration for authentication:
app/Models/User.php
Next Steps
Laravel Sanctum Setup
Learn how to configure Sanctum and generate API tokens
API Resources
Explore the available API endpoints