Skip to main content
POST
/
api
/
auth
/
register
Register User
curl --request POST \
  --url https://api.example.com/api/auth/register \
  --header 'Content-Type: application/json' \
  --data '
{
  "username": "<string>",
  "email": "<string>",
  "password": "<string>"
}
'
{
  "message": "Please provide username, email and password"
}

Description

Creates a new user account with username, email, and password. Upon successful registration, a JWT token is automatically set as an HTTP-only cookie with a 1-day expiration.

Request Body

username
string
required
The unique username for the new user account
email
string
required
The email address for the new user account. Must be unique.
password
string
required
The password for the new user account. Will be hashed using bcrypt before storage.

Response

message
string
Success message indicating the user was registered successfully
user
object
The registered user object

Success Response (201)

{
  "message": "User registered successfully",
  "user": {
    "id": "507f1f77bcf86cd799439011",
    "username": "johndoe",
    "email": "[email protected]"
  }
}
A JWT token is automatically set as an HTTP-only cookie named token with a 1-day expiration. The token contains:
  • id: User’s unique identifier
  • username: User’s username
  • exp: Token expiration timestamp

Error Responses

{
  "message": "Please provide username, email and password"
}

Example Request

curl -X POST https://api.example.com/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "username": "johndoe",
    "email": "[email protected]",
    "password": "securePassword123"
  }'

Implementation Details

  • Passwords are hashed using bcrypt with a salt round of 10 before storage
  • JWT tokens are signed with JWT_SECRET environment variable
  • Token expiration is set to 1 day (24 hours)
  • The endpoint checks for duplicate usernames and email addresses
  • No authentication required (public endpoint)

Build docs developers (and LLMs) love