Skip to main content

Overview

The user registration endpoint allows new users to create accounts in the system. All passwords are securely hashed using bcrypt before storage.

Endpoint

POST /register

Request Body

FieldTypeRequiredDescription
documentstringYesUser’s identification document
emailstringYesUser’s email address (must be unique)
passwordstringYesUser’s password (will be hashed)
namestringYesUser’s first name
last_namestringYesUser’s last name
cellphonestringYesUser’s phone number
user_typestringYesUser type or role
Both email and document must be unique in the system. Registration will fail if either already exists.

Implementation Details

Password Hashing

The system uses bcrypt to hash passwords before storage:
userController.js:22
const hashedPassword = await bcrypt.hash(password, 10);
The bcrypt salt round is set to 10, providing a strong balance between security and performance.

Validation Process

1

Required Fields Check

The system validates that email and password are provided.
userController.js:8-10
if (!email || !password) {
  return res.status(400).json({ message: 'Email y contraseña son obligatorios' });
}
2

Email Uniqueness

Check if the email is already registered in the system.
userController.js:12-15
const existingUser = await User.findOne({ email });
if (existingUser) {
  return res.status(400).json({ message: 'El correo electrónico ya está registrado' });
}
3

Document Uniqueness

Verify that the document number is not already in use.
userController.js:17-20
const existingDocument = await User.findOne({ document });
if (existingDocument) {
  return res.status(400).json({ message: 'La cédula ya está registrada' });
}
4

User Creation

Create and save the new user with hashed password.
userController.js:24-34
const newUser = new User({
  document,
  email,
  password: hashedPassword,
  name,
  last_name,
  cellphone,
  user_type,
});

await newUser.save();

Examples

const response = await fetch('http://localhost:3000/register', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    document: '1234567890',
    email: '[email protected]',
    password: 'SecurePassword123!',
    name: 'John',
    last_name: 'Doe',
    cellphone: '+1234567890',
    user_type: 'contractor'
  })
});

const data = await response.json();
console.log(data);

Response

Success Response (201 Created)

{
  "message": "Usuario registrado correctamente",
  "user": {
    "document": "1234567890",
    "email": "[email protected]",
    "password": "$2a$10$XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx",
    "name": "John",
    "last_name": "Doe",
    "cellphone": "+1234567890",
    "user_type": "contractor",
    "_id": "507f1f77bcf86cd799439011",
    "__v": 0
  }
}
The password in the response is the hashed version. However, in production, it’s recommended to exclude the password field from API responses entirely.

Error Responses

Missing Required Fields (400 Bad Request)

{
  "message": "Email y contraseña son obligatorios"
}

Duplicate Email (400 Bad Request)

{
  "message": "El correo electrónico ya está registrado"
}

Duplicate Document (400 Bad Request)

{
  "message": "La cédula ya está registrada"
}

Server Error (500 Internal Server Error)

{
  "message": "Error al registrar el usuario",
  "error": "Detailed error message"
}

Edge Cases

Important edge cases to handle:
  • Empty strings: While fields may be provided, empty strings will pass the basic validation but may cause issues. Consider adding length validation.
  • Invalid email format: The current implementation doesn’t validate email format. Consider adding email validation.
  • Weak passwords: No password strength requirements are enforced. Consider implementing password policies.
  • Document format: No validation on document format or length.

Best Practices

  1. Client-side validation: Validate input on the client side before sending requests
  2. Password requirements: Implement minimum password length and complexity rules
  3. Email verification: Consider adding email verification flow after registration
  4. Rate limiting: Implement rate limiting to prevent abuse
  5. Sanitization: Sanitize user input to prevent injection attacks

Build docs developers (and LLMs) love