Authentication

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

  1. Store Access Tokens Securely: Use secure storage (not localStorage in production)

  2. Handle Token Expiration: Implement automatic refresh logic

  3. Secure Cookies: Refresh tokens are stored in HTTP-only, secure cookies

  4. Logout Properly: Always call logout endpoint to invalidate refresh tokens

  5. HTTPS Only: Use HTTPS in production for token security

Role-Based Access

User Role (user)

  • Can manage own profile

  • Can create, view, and manage own comments

  • Can like/unlike blog posts

  • Can view published blog posts

Admin Role (admin)

  • All user permissions

  • Can view all users and manage user accounts

  • Can create, edit, and delete blog posts

  • Can view draft blog posts

  • Can delete any comments

Common Authentication Errors

Status Code
Error Code
Description
Solution

400

ValidationError

Invalid email/password format

Check request format

401

AuthenticationError

Missing/invalid/expired token

Refresh token or re-login

403

AuthorizationError

Insufficient permissions

Check user role

403

AuthorizationError

Admin registration denied

Contact admin for whitelist

Code Examples

Complete Authentication Implementation

Last updated