Skip to main content

Endpoint

POST /register
Creates a new user account with email and password. The password is hashed using bcrypt with a cost factor of 14 before being stored in the database.

Request

email
string
required
User’s email address. Must be unique.
password
string
required
User’s password. Will be hashed with bcrypt before storage.

Request Format

The endpoint expects form data (application/x-www-form-urlencoded or multipart/form-data).

Response

status
number
HTTP status code: 201 for success, 401 for errors
body
string
Returns "Success" on successful registration

Example Request

curl -X POST http://localhost:8080/register \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "[email protected]" \
  -d "password=securePassword123"

Example Responses

"Success"
{
  "error": "user already exists"
}
Common error scenarios:
  • Email already registered
  • Database connection issues
  • Invalid email format

Implementation Details

User Creation Process

  1. UUID Generation: A unique UUID v4 is generated as the user ID
  2. Password Hashing: Password is hashed using bcrypt with cost factor 14
  3. Default Status: New users are created with status: "pending"
  4. Timestamp: created_at is set to the current time
  5. Database Storage: User document is inserted into MongoDB users collection

User Object Structure

The created user has the following initial structure:
User{
  ID:        "<uuid-v4>",
  Email:     "<provided-email>",
  Password:  []byte("<bcrypt-hash>"),
  Status:    "pending",
  CreatedAt: time.Now(),
}

Source Code References

  • Handler: backend/auth/routes.go:29 - HandleUserRegistration
  • Logic: backend/auth/controller.go:30 - createNewUser
  • Model: backend/users/model.go:24 - CreateNewUser

Error Handling

Errors are returned as JSON with HTTP status 401:
  • Duplicate Email: MongoDB unique constraint violation
  • Hashing Failure: bcrypt.GenerateFromPassword error
  • Database Error: MongoDB insertion failures

Security Considerations

  • Passwords are never stored in plain text
  • bcrypt cost factor of 14 provides strong security
  • Email uniqueness is enforced at the database level
  • New accounts start in “pending” status for verification workflows

Build docs developers (and LLMs) love