Overview
SushiGo uses Laravel Passport for OAuth2 authentication with a password grant flow. This allows users to authenticate with email/phone and password to receive access tokens for API requests.Authentication Flow
Password Grant Implementation
Login Request
Endpoint:POST /api/v1/auth/login
Request body:
Controller Implementation
Fromcode/api/app/Http/Controllers/Api/V1/Auth/LoginController.php:
Token Management
Access Tokens
Characteristics:- Bearer tokens (JWT format)
- Default expiration: 1 year (31536000 seconds)
- Stored in
oauth_access_tokenstable - Passed in
Authorizationheader
Automatic Token Injection
The webapp’s API client automatically injects the token:Login/Logout Endpoints
Login
POST/api/v1/auth/login
Request:
Logout
POST/api/v1/auth/logout
Headers:
Get Current User
GET/api/v1/auth/me
Returns the authenticated user’s profile and roles.
Headers:
Token Refresh
Laravel Passport tokens have a 1-year expiration by default. Token refresh is not currently implemented, but can be added using OAuth2’srefresh_token grant.
Future implementation:
Token expiration is configured in
config/passport.php:Password Reset Flow
1. Request Reset Token
POST/api/v1/auth/forgot-password
2. Verify Reset Token
POST/api/v1/auth/verify-reset-token
3. Reset Password
POST/api/v1/auth/reset-password
User Model
TheUser model integrates Laravel Passport and Spatie Permissions:
HasApiTokens- Laravel Passport integrationHasRoles- Spatie Permissions role managementNotifiable- Email/SMS notifications
Protected Routes
All protected routes use theauth:api middleware:
- Extract token from
Authorizationheader - Query
oauth_access_tokenstable - Load associated User model
- Make user available via
$request->user()orauth()->user() - Reject request with 401 if token invalid/expired
Frontend Login Implementation
Security Considerations
Token Storage
Token Storage
- Tokens stored in localStorage (persistent across sessions)
- Alternative: sessionStorage (cleared on tab close)
- Consider httpOnly cookies for enhanced security
- Never expose tokens in URLs or logs
HTTPS Required
HTTPS Required
- All authentication must occur over HTTPS
- Tokens transmitted in plaintext in Authorization header
- Production deployment requires SSL/TLS certificates
Rate Limiting
Rate Limiting
- Login endpoint should be rate-limited
- Laravel default: 5 attempts per minute per email
- Configure in
app/Http/Kernel.phpthrottle middleware
Token Revocation
Token Revocation
- Logout revokes all user tokens
- Manual revocation via
User::tokens()->delete() - Expired tokens automatically cleaned by Passport
Development Users
Default test accounts (from seeders):| Password | Role | |
|---|---|---|
| [email protected] | admin123456 | super-admin |
| [email protected] | admin123456 | admin |
| [email protected] | inventory123456 | inventory-manager |
Troubleshooting
401 Unauthorized
Cause: Token invalid, expired, or missing Solutions:- Verify token in Authorization header:
Bearer {token} - Check token hasn’t expired (1 year default)
- Ensure user still exists and is active
- Verify Passport tables exist:
oauth_access_tokens,oauth_clients
Token Not Found in Database
Cause: Passport not installed or migrations not run Solution:CORS Errors
Cause: Frontend and API on different domains Solution: Configure CORS inconfig/cors.php:
Related Documentation
Permissions
Role-based authorization
User Management
Creating and managing users
API Reference
Auth endpoint documentation
System Architecture
Overall system design