Eco-It’s local authentication flow keeps account creation safe by requiring every new user to verify their email address before their account is created in the database. A temporaryDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/vanegasjoseignacio2-cyber/Eco-It/llms.txt
Use this file to discover all available pages before exploring further.
PendingRegistration document holds the user’s details and a hashed 6-digit code for up to 15 minutes. Only after a successful code verification is the permanent User document written to MongoDB and a JWT issued. This page documents every endpoint, field requirement, and error case in the local auth flow.
Registration flow
Registration is a two-request handshake. The first request sends a verification code to the user’s email; the second verifies that code and creates the account. A separate resend endpoint lets users request a fresh code if theirs expires or gets lost.Send the verification code
Submit the user’s full registration details to request a 6-digit code. The backend validates all fields, checks that the email is not already registered, hashes the password, and emails the code before writing anything to the database — if the email delivery fails, no record is created.Endpoint: Success response
POST /api/auth/enviar-codigo-registroRequest body:First name. 2–50 characters.
Last name.
Valid email address. Must not already have an active account.
Plain-text password. Minimum 8 characters (enforced by the
User model schema). The backend hashes it with bcrypt (salt rounds: 12) before storing in PendingRegistration.Mobile phone number. Validated with
isMobilePhone().User’s age. Stored as an integer.
200:A 3-minute cooldown applies between code requests for the same email. If a pending registration already exists with more than 12 minutes remaining on its 15-minute timer (i.e., fewer than 3 minutes have passed since the last request), the server returns
429 with the number of minutes to wait.Verify the code and create the account
Submit the email and the 6-digit code from the email. The backend hashes the submitted code with SHA-256 and compares it against the stored hash. On success, it creates the Success response
User document, deletes the PendingRegistration record, sends a welcome email, and returns a JWT.Endpoint: POST /api/auth/verificar-registroThe same email used in step 1.
The 6-digit verification code received by email.
201:Resend the verification code
If the code was not received or has expired, users can request a new one without resubmitting their full details.Endpoint: Success response
POST /api/auth/reenviar-codigo-registroEmail address of the pending registration.
200:Login
After an account is created (or on subsequent visits), users authenticate by submitting their email and password. The backend uses bcrypt’scompare to validate the password against the hashed value stored in MongoDB (the password field is excluded from queries by default via select: false in the schema — the login controller explicitly re-includes it with .select('+password')).
Endpoint: POST /api/auth/login
Registered email address.
Plain-text password.
200:
A signed JWT valid for 12 hours. Store this in
localStorage under the key token and include it in subsequent requests as Authorization: Bearer <token>.The authenticated user’s profile data, excluding the password hash.
If the account was created with Google OAuth and has no local password set, the login endpoint returns
401 with the message: “Este usuario se registró con Google. Por favor, inicia sesión con Google.” Direct users to the Google OAuth flow in this case.Logout
Logout is handled client-side by removing the token fromlocalStorage. The server endpoint exists for audit-log purposes and to invalidate any future server-side session state.
Endpoint: POST /api/auth/logout
200:
Validation rules
ThevalidarRegistro and validarLogin middlewares from validation.js run before the controllers and return a 400 response with a structured errores array if any field is invalid.
Registration (validarRegistro)
| Field | Rule |
|---|---|
nombre | Required · 2–50 characters (trimmed) |
email | Required · valid email format · normalized to lowercase |
password | Required · minimum 6 characters (middleware) — the User schema itself enforces 8 characters as a floor |
The
validarRegistro middleware is attached to the legacy POST /api/auth/registro route, which now returns 400 directing clients to use the two-step email verification flow instead. The enviarCodigoRegistro controller performs its own inline field checks for the full set of registration fields (nombre, apellido, edad, email, telefono, password).Login (validarLogin)
| Field | Rule |
|---|---|
email | Required · valid email format (trimmed) |
password | Required (non-empty) |
Profile completion (validarPerfilCompleto)
| Field | Rule |
|---|---|
apellido | Required (trimmed, non-empty) |
edad | Integer between 1 and 120 |
telefono | Required · valid mobile phone format |
Password hashing
Eco-It uses bcrypt with a salt factor of 10 for all local passwords. Hashing is performed in a Mongoose
pre('save') hook on the User model. The hook is skipped when this.$skipPasswordHash is set to true (used during email-verification registration, where the password is already hashed in PendingRegistration) or when the password field has not been modified.Profile completion (for Google OAuth users)
Google OAuth users are created withoutedad or telefono, and apellido is only set when Google returns a family name. Their perfilCompleto flag is always set to false at creation. After their first login they must complete their profile before accessing the full platform. This endpoint is also available to any authenticated user who needs to update these fields.
Endpoint: PUT /api/auth/completar-perfil
Requires: Authorization: Bearer <token>
Last name (trimmed, non-empty).
Integer between 1 and 120.
Valid mobile phone number.
200: