JWT-based authentication endpoints for user registration and login.
Create a new user account.
Endpoint: POST /api/v1/auth/register
Headers:
Content-Type: application/jsonRequest Body:
{
"email": "user@example.com",
"password": "SecurePass123"
}Validation Rules:
email: Valid email formatpassword: Minimum 8 characters, must contain uppercase, lowercase, and number
Response: 201 Created
{
"user": {
"id": 1,
"email": "user@example.com"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}Errors:
400: Validation failed409: User already exists
Authenticate and receive JWT token.
Endpoint: POST /api/v1/auth/login
Request Body:
{
"email": "user@example.com",
"password": "SecurePass123"
}Response: 200 OK
{
"user": {
"id": 1,
"email": "user@example.com"
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}Errors:
400: Validation failed401: Invalid credentials
Get current user's profile.
Endpoint: GET /api/v1/auth/profile
Headers:
Authorization: Bearer <token>Response: 200 OK
{
"id": 1,
"email": "user@example.com",
"created_at": "2025-10-13T12:00:00.000Z"
}Errors:
401: Invalid or expired token
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9. # Header
eyJ1c2VySWQiOjEsImlhdCI6MTYzMDAwMDAwMH0. # Payload
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c # Signature
{
"userId": 1,
"iat": 1630000000,
"exp": 1630604800
}- Algorithm: HS256 (enforced)
- Expiration: 7 days
- Secret: From
JWT_SECRETenvironment variable
Include in Authorization header:
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...- Algorithm: bcrypt
- Rounds: 12
- Salt: Automatically generated
- Algorithm whitelisting (only HS256)
- Expiration enforcement
- Signature validation
Generic errors to prevent user enumeration:
- ✅ "Invalid credentials"
- ❌ "User not found"
- ❌ "Wrong password"
Register:
curl -X POST http://localhost:3000/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"password": "TestPass123"
}'Login:
curl -X POST http://localhost:3000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"password": "TestPass123"
}'Get Profile:
TOKEN="your-jwt-token"
curl http://localhost:3000/api/v1/auth/profile \
-H "Authorization: Bearer $TOKEN"// Register
const response = await fetch('/api/v1/auth/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'test@example.com',
password: 'TestPass123'
})
});
const { user, token } = await response.json();
localStorage.setItem('token', token);
// Use token
const profile = await fetch('/api/v1/auth/profile', {
headers: { 'Authorization': `Bearer ${token}` }
});interface AuthApi {
@POST("auth/register")
suspend fun register(@Body request: RegisterRequest): AuthResponse
@POST("auth/login")
suspend fun login(@Body request: LoginRequest): AuthResponse
@GET("auth/profile")
suspend fun getProfile(): UserProfile
}
// Usage
val response = api.register(RegisterRequest(email, password))
secureStorage.saveToken(response.token)See Also: