This document covers all authentication-related endpoints in the Chirpy API.
Chirpy uses JWT (JSON Web Tokens) for authentication with a refresh token system:
- Access tokens expire in 1 hour and are used for API requests
- Refresh tokens expire in 60 days and are used to obtain new access tokens
- Passwords are hashed using bcrypt
All authentication endpoints are prefixed with /api
Authenticate a user and receive access and refresh tokens.
Request Body:
{
"email": "[email protected]",
"password": "securepassword123"
}Response (200 OK):
{
"id": "123e4567-e89b-12d3-a456-426614174000",
"email": "[email protected]",
"is_chirpy_red": false,
"created_at": "2023-01-01T00:00:00Z",
"updated_at": "2023-01-01T00:00:00Z",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "abc123def456..."
}Error Responses:
400 Bad Request- Invalid JSON or missing email/password401 Unauthorized- Incorrect email or password500 Internal Server Error- Server error
Example:
curl -X POST http://localhost:8080/api/login \
-H "Content-Type: application/json" \
-d '{"email": "[email protected]", "password": "password123"}'Get a new access token using a refresh token.
Request Headers:
Authorization: Bearer <refresh_token>
Response (200 OK):
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}Error Responses:
401 Unauthorized- Invalid, expired, or missing refresh token500 Internal Server Error- Server error
Example:
curl -X POST http://localhost:8080/api/refresh \
-H "Authorization: Bearer abc123def456..."Revoke (invalidate) a refresh token.
Request Headers:
Authorization: Bearer <refresh_token>
Response (204 No Content): Empty response body
Error Responses:
401 Unauthorized- Invalid or missing refresh token500 Internal Server Error- Server error
Example:
curl -X POST http://localhost:8080/api/revoke \
-H "Authorization: Bearer abc123def456..."First, create a user account using the Users API.
Use the /api/login endpoint with email and password to receive:
- Access token (for API requests)
- Refresh token (for renewing access tokens)
Include the access token in the Authorization header:
Authorization: Bearer <access_token>
When the access token expires (after 1 hour), use the refresh token to get a new access token via /api/refresh.
Revoke the refresh token using /api/revoke to log out the user.
- Password Requirements: While not enforced by the API, use strong passwords
- Token Storage: Store tokens securely (avoid localStorage for sensitive apps)
- HTTPS: Always use HTTPS in production
- Token Expiration: Access tokens expire in 1 hour, refresh tokens in 60 days
- Password Hashing: Passwords are hashed using bcrypt with default cost
All authentication endpoints return appropriate HTTP status codes:
200 OK- Success204 No Content- Success (no response body)400 Bad Request- Invalid request data401 Unauthorized- Authentication failed500 Internal Server Error- Server error
Error responses include a plain text message in the response body.