The Blog API uses JSON Web Tokens (JWT) for authentication, implementing a dual-token strategy with access tokens and refresh tokens for enhanced security.
Overview
Access Token: Short-lived JWT used for API requests (sent in Authorization header)
Refresh Token: Long-lived JWT stored in HTTP-only cookie for token renewal
Security: Role-based access control with 'admin' and 'user' roles
Endpoints
Register User
Creates a new user account with optional role specification.
Endpoint: POST /auth/register
Authentication: None required
Request Body:
{"email":"user@example.com","password":"securepassword123","role":"user"// Optional: "user" or "admin"}
Success Response (201 Created):
Error Responses:
400 - Validation errors (invalid email, password too short)
403 - Admin registration denied for non-whitelisted email
500 - Internal server error
Notes:
Password must be at least 8 characters
Admin registration requires email to be on server whitelist
Sets refreshToken HTTP-only cookie automatically
Login User
Authenticates existing user and returns access token.
Endpoint: POST /auth/login
Authentication: None required
Request Body:
Success Response (200 OK):
Error Responses:
400 - Invalid credentials or validation errors
404 - User not found
500 - Internal server error
Refresh Access Token
Generates a new access token using the refresh token cookie.
Endpoint: POST /auth/refresh-token
Authentication: Requires refreshToken cookie (sent automatically by browser)
Request Body: None
Success Response (200 OK):
Error Responses:
400 - Missing or invalid refresh token cookie
401 - Expired or invalid refresh token
500 - Internal server error
Usage Example:
Logout User
Invalidates refresh token and clears cookie.
Endpoint: POST /auth/logout
Authentication: Requires both access token (header) and refresh token (cookie)
Headers:
Request Body: None
Success Response (200 OK):
Error Responses:
400 - Missing refresh token cookie
401 - Invalid or expired tokens
500 - Internal server error
Authentication Flow
1. Initial Authentication
2. Making Authenticated Requests
3. Token Refresh Flow
4. Logout
Security Best Practices
Store Access Tokens Securely: Use secure storage (not localStorage in production)
// When your access token expires (401 error), call refresh endpoint
const response = await fetch("/api/v1/auth/refresh-token", {
method: "POST",
credentials: "include", // Important: includes cookies
});
if (response.ok) {
const { accessToken } = await response.json();
// Use new access token for subsequent requests
localStorage.setItem("accessToken", accessToken);
}
Authorization: Bearer <access_token>
{
"message": "Logged out successfully"
}
sequenceDiagram
participant Client
participant API
participant Database
Client->>API: POST /auth/register or /auth/login
API->>Database: Validate credentials/create user
Database-->>API: User data
API-->>Client: Access token + Set refresh cookie
// Include access token in all protected requests
const response = await fetch("/api/v1/users/current", {
method: "GET",
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
});
sequenceDiagram
participant Client
participant API
Client->>API: Protected request with expired token
API-->>Client: 401 Unauthorized
Client->>API: POST /auth/refresh-token (with cookie)
API-->>Client: New access token
Client->>API: Retry original request with new token
API-->>Client: Success response