API Authentication ensures that only authorized users can access protected API endpoints.
- Sanctum
- Passport
- JWT in laravel
1. Laravel Sanctum:
Sanctum is Laravel's lightweight authentication package for SPA, mobile apps, and simple APIs.
$token = $user->createToken('myToken')->plainTextToken;User Login
│
▼
Sanctum Generates Token
│
▼
Client Stores Token
│
▼
Send Token with Every RequestBest For:
- Mobile Apps
- React
- Vue
- Angular
- Simple REST APIs
2. Passport:
Passport is Laravel's full OAuth2 authentication package.
It supports:
- OAuth2
- Access Token
- Refresh Token
- Client Credentials
User Login
│
▼
Passport Creates Access Token
│
▼
Client Uses Access Token
│
▼
Laravel Verifies TokenBest For
- Large Applications
- Third-party Login
- OAuth2
- Enterprise APIs
3. JWT (JSON Web Token)
JWT is a token-based authentication system where user information is stored inside a signed token.
User Login
│
▼
JWT Token Generated
│
▼
Client Stores Token
│
▼
Bearer Token Sent
│
▼
Laravel Verifies JWTBest For
- REST APIs
- Microservices
- Cross-platform applications
| Feature | Sanctum | Passport | JWT |
|---|---|---|---|
| Type | Simple Token Authentication | OAuth2 Authentication | JSON Web Token |
| Complexity | Easy | Advanced | Medium |
| Uses Database | ✅ Yes (stores tokens) | ✅ Yes | ❌ No (token contains data) |
| Best For | SPA, Mobile Apps, Simple APIs | Enterprise APIs, OAuth2 | REST APIs, Microservices |
| Laravel Package | Official | Official | Third-party package (tymon/jwt-auth) |