v2/examples/05-swarm-apps/rest-api-advanced/README-AUTH.md
This REST API includes a comprehensive authentication system with JWT tokens, refresh tokens, email verification, and password reset functionality.
POST /api/auth/register
Body:
{
"email": "[email protected]",
"password": "SecurePass123",
"confirmPassword": "SecurePass123",
"name": "John Doe",
"phone": "+1234567890",
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zipCode": "10001",
"country": "USA"
}
}
POST /api/auth/login
Body:
{
"email": "[email protected]",
"password": "SecurePass123",
"rememberMe": true
}
POST /api/auth/logout
Authorization: Bearer <token>
POST /api/auth/refresh
Body:
{
"refreshToken": "<refresh_token>"
}
POST /api/auth/forgot-password
Body:
{
"email": "[email protected]"
}
POST /api/auth/reset-password
Body:
{
"token": "<reset_token>",
"password": "NewSecurePass123",
"confirmPassword": "NewSecurePass123"
}
GET /api/auth/verify-email/:token
POST /api/auth/resend-verification
Authorization: Bearer <token>
GET /api/auth/me
Authorization: Bearer <token>
POST /api/auth/check-password
Body:
{
"password": "TestPassword123!"
}
GET /api/users?page=1&limit=20&sort=-createdAt&role=user&search=john
Authorization: Bearer <admin_token>
GET /api/users/stats
Authorization: Bearer <admin_token>
GET /api/users/:id
Authorization: Bearer <token>
PUT /api/users/:id
Authorization: Bearer <token>
Body:
{
"name": "Jane Doe",
"phone": "+0987654321",
"address": {
"street": "456 Oak Ave",
"city": "Los Angeles",
"state": "CA"
},
"avatar": "https://example.com/avatar.jpg"
}
DELETE /api/users/:id
Authorization: Bearer <token>
PUT /api/users/change-password
Authorization: Bearer <token>
Body:
{
"currentPassword": "OldPassword123",
"newPassword": "NewPassword123",
"confirmPassword": "NewPassword123"
}
PUT /api/users/change-email
Authorization: Bearer <token>
Body:
{
"newEmail": "[email protected]",
"password": "CurrentPassword123"
}
GET /api/users/:id/activity?page=1&limit=50
Authorization: Bearer <token>
Required environment variables for authentication:
# JWT Configuration
JWT_SECRET=your-super-secret-jwt-key
JWT_EXPIRE=7d
JWT_COOKIE_EXPIRE=7
# Security
BCRYPT_SALT_ROUNDS=10
# Features
ENABLE_EMAIL_VERIFICATION=true
SOFT_DELETE=true
ROTATE_REFRESH_TOKENS=true
# Email (for password reset and verification)
EMAIL_HOST=smtp.mailtrap.io
EMAIL_PORT=2525
EMAIL_USER=your-email-user
EMAIL_PASS=your-email-password
[email protected]
The authentication system returns appropriate HTTP status codes:
200: Success201: Created (registration)400: Bad Request (validation errors)401: Unauthorized (invalid credentials)403: Forbidden (insufficient permissions)404: Not Found (user not found)Error responses include detailed messages:
{
"success": false,
"error": "Validation Error",
"errors": {
"body": {
"email": "Please provide a valid email address",
"password": "Password must be at least 6 characters long"
}
}
}
Start the server:
npm run dev
Register a new user:
curl -X POST http://localhost:3000/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "Test123!",
"confirmPassword": "Test123!",
"name": "Test User"
}'
Login:
curl -X POST http://localhost:3000/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "Test123!"
}'
Use the access token for protected routes:
curl -X GET http://localhost:3000/api/auth/me \
-H "Authorization: Bearer <access_token>"
The current implementation includes mock email sending. For production, integrate with:
Redis is used for:
Ensure the following indexes are created for optimal performance: