Skip to main content

Overview

The Authentication API provides endpoints for user registration, login, and retrieving the current user’s information. All authentication uses JWT (JSON Web Tokens) for secure session management.

POST /api/auth/register

Register a new user account.

Request

Method: POST Path: /api/auth/register Authentication: None required Headers:
  • Content-Type: application/json
Body Parameters:
email
string
required
User’s email address. Must be unique.
password
string
required
User’s password. Will be hashed using bcrypt.
name
string
required
User’s full name.

Response

message
string
Success message
user
object
User object containing user details
user.id
integer
User’s unique identifier
user.email
string
User’s email address
user.name
string
User’s full name
user.role
string
User’s role (default: “customer”)
user.created_at
datetime
Account creation timestamp
user.updated_at
datetime
Last update timestamp
access_token
string
JWT access token for authenticated requests

Example

cURL
curl -X POST http://localhost:5000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "[email protected]",
    "password": "securePassword123",
    "name": "John Doe"
  }'
Response (201 Created):
{
  "message": "User registered successfully",
  "user": {
    "id": 1,
    "email": "[email protected]",
    "name": "John Doe",
    "role": "customer",
    "created_at": "2024-01-15T10:30:00",
    "updated_at": "2024-01-15T10:30:00"
  },
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

Error Responses

400 Bad Request - Missing required fields:
{
  "error": "Missing required fields: email, password, name"
}
409 Conflict - Email already registered:
{
  "error": "Email already registered"
}

POST /api/auth/login

Authenticate an existing user.

Request

Method: POST Path: /api/auth/login Authentication: None required Headers:
  • Content-Type: application/json
Body Parameters:
email
string
required
User’s registered email address
password
string
required
User’s password

Response

message
string
Success message
user
object
User object containing user details (same structure as registration)
access_token
string
JWT access token for authenticated requests

Example

cURL
curl -X POST http://localhost:5000/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "[email protected]",
    "password": "securePassword123"
  }'
Response (200 OK):
{
  "message": "Login successful",
  "user": {
    "id": 1,
    "email": "[email protected]",
    "name": "John Doe",
    "role": "customer",
    "created_at": "2024-01-15T10:30:00",
    "updated_at": "2024-01-15T10:30:00"
  },
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

Error Responses

400 Bad Request - Missing required fields:
{
  "error": "Missing required fields: email, password"
}
401 Unauthorized - Invalid credentials:
{
  "error": "Invalid email or password"
}

GET /api/auth/me

Get the current authenticated user’s information.

Request

Method: GET Path: /api/auth/me Authentication: JWT token required Headers:
  • Authorization: Bearer <access_token>

Response

user
object
User object containing current user’s details
user.id
integer
User’s unique identifier
user.email
string
User’s email address
user.name
string
User’s full name
user.role
string
User’s role
user.created_at
datetime
Account creation timestamp
user.updated_at
datetime
Last update timestamp

Example

cURL
curl -X GET http://localhost:5000/api/auth/me \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
Response (200 OK):
{
  "user": {
    "id": 1,
    "email": "[email protected]",
    "name": "John Doe",
    "role": "customer",
    "created_at": "2024-01-15T10:30:00",
    "updated_at": "2024-01-15T10:30:00"
  }
}

Error Responses

401 Unauthorized - Missing or invalid token:
{
  "msg": "Missing Authorization Header"
}
404 Not Found - User not found:
{
  "error": "User not found"
}

Authentication Flow

  1. Register a new account using /api/auth/register or login with existing credentials using /api/auth/login
  2. Save the access_token from the response
  3. Include the token in the Authorization header for all protected endpoints:
    Authorization: Bearer <access_token>
    
  4. Use /api/auth/me to verify the token and retrieve current user information

Build docs developers (and LLMs) love