Implement User Registration API endpoint#62
Open
arandomogg wants to merge 1 commit into
Open
Conversation
Add a POST /api/v1/auth/register endpoint following the Controller -> Service -> Model architecture. Passwords are securely hashed with bcrypt and the hash is never returned to clients. - User model with schema validation, a unique email index, a bcrypt pre-save hook, a comparePassword method, and a toJSON transform that strips the password hash and exposes id instead of _id - authService.registerUser handles duplicate-email detection (including the unique-index race condition) and returns sanitized user data - authController.register validates input and returns 201 with the user - authValidator performs strict, typed validation of the request body - ApiError and asyncHandler utilities for consistent error handling - Wire auth routes under /api/v1/auth - Integration tests covering success, duplicate email, and validation Closes SwiftChainn#9
|
@arandomogg Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the user registration endpoint following the strict Controller -> Service -> Model architecture. Passwords are hashed with bcrypt at the model layer, and the password hash is never exposed in any response.
Endpoint
POST /api/v1/auth/registerRequest body:
{ "name": "Ada Lovelace", "email": "ada@example.com", "password": "sup3rSecret!" }Success response (
201 Created):{ "status": "success", "message": "User registered successfully", "data": { "user": { "id": "6a41ad5e9d50a2b2514c3b88", "name": "Ada Lovelace", "email": "ada@example.com", "role": "user", "createdAt": "2026-06-28T23:25:18.892Z", "updatedAt": "2026-06-28T23:25:18.892Z" } } }Changes
src/models/User.ts: MongooseUsermodel with schema validation, a unique email index, a bcrypt pre-save hook (configurable viaBCRYPT_ROUNDS), acomparePasswordinstance method, and atoJSONtransform that removes the password hash and__vand exposesidinstead of_id.src/services/authService.ts:registerUserbusiness logic. Detects duplicate emails (including the unique-index race condition via the11000error code) and returns a sanitized user object.src/controllers/authController.ts: HTTP controller that validates the body, invokes the service, and returns201.src/validators/authValidator.ts: Strict, strongly-typed validation of the request body.src/utils/ApiError.tsandsrc/utils/asyncHandler.ts: Reusable error type and async wrapper for consistent error handling through the existing global error middleware.src/routes/authRoutes.ts/src/routes/index.ts: Mounts the auth router under/api/v1/auth.tests/auth.test.ts: Integration tests using an in-memory MongoDB.Acceptance criteria
/api/v1.Proof of work
All unit/integration tests pass:
npx tsc(build) andeslinton the changed files both pass cleanly.Closes #9