Skip to content

Implement User Registration Endpoint for REST API #1

@emanuelwellyngton

Description

@emanuelwellyngton

Add an endpoint to the Spring Boot REST API for user registration that will create both the User and the Profile records in a single request. This endpoint will ensure that the user and profile are created together in a transactional manner, ensuring atomicity and data consistency. The user registration process will also pre-validate if the email is already registered, generate a verification code, and send it to the user’s email.

Requirements:

  1. User Registration Endpoint:

    • Implement a POST API endpoint for user registration (e.g., /api/auth/register).
    • The endpoint should accept the following fields in the request body:
      • email (String)
      • password (String)
      • fullName (String)
      • profileData (Profile-related fields, e.g., bio, profilePictureUrl, etc.)
  2. Account Already Registered Check:

    • Pre-validate that the email provided is not already registered by checking the existing users.
    • If the email is already registered, return an appropriate error response (e.g., 400 Bad Request with a message like "Email already in use").
  3. User and Profile Creation:

    • User Record: Create the user record in the database with the provided email and password. Store the password securely (using hashing).
    • Profile Record: Create the associated profile record, including fields like bio, profilePictureUrl, and any other relevant information.
    • Ensure both the user and profile records are created in the same transaction (atomic operation).
  4. Verification Code Generation:

    • Generate a verification code for the user and store it in the User entity along with an expiration date.
    • Send the verification code to the provided email using an email service.
  5. Response:

    • If the user and profile are successfully created, return a success message with a 201 Created status.
    • In case of errors (e.g., email already in use), return an appropriate error message with a proper HTTP status code (e.g., 400 Bad Request).
  6. User Model & Profile Model:

    • Ensure the User entity has the isVerified flag to indicate if the user has verified their account.
    • The User entity should store the verificationCode and expirationDate (timestamp) to manage code expiration.
    • The Profile entity should store profile-related data like bio, profilePictureUrl, etc.
  7. Security:

    • Ensure the password is hashed and not stored in plain text.
    • Ensure that the verification code is not exposed in the response or logs.
  8. Testing:

    • Write unit and integration tests for the user registration endpoint.
    • Test for various scenarios (successful registration, email already in use, email sending failure, etc.).

Tasks:

  • Implement the POST /api/auth/register endpoint to create both the user and profile records.
  • Pre-validate that the email is not already registered.
  • Hash the password securely before saving it in the user record.
  • Generate and send the verification code to the user's email.
  • Handle error scenarios (e.g., email already in use, email sending failure).
  • Write unit and integration tests for the user registration endpoint.
  • Ensure security best practices are followed when creating the user and profile records.
  • Add Swagger/OpenAPI documentation for the user registration endpoint.

Acceptance Criteria:

  • A successful registration request results in the user and profile being created, the user being assigned a verification code, and an email being sent with the verification code.
  • If the email is already in use, the response returns a 400 Bad Request status with an appropriate message.
  • If the email fails to send, the response returns a 500 Internal Server Error with a message indicating the failure.
  • The user’s password is securely hashed.
  • All relevant tests pass successfully.

Additional Notes:

  • Follow the project’s existing conventions for exception handling, response formatting, and logging.
  • Ensure that the user and profile records are created in a single transaction to maintain atomicity.
  • Add relevant Swagger/OpenAPI documentation for the new user registration endpoint.

Metadata

Metadata

Labels

Type

No type

Projects

Status

In progress

Relationships

None yet

Development

No branches or pull requests

Issue actions