hngprojects / hng_boilerplate_nestjs

Description
Apache License 2.0
182 stars 105 forks source link

[FEAT]: Implement API Endpoint for User Password Reset - Backend #51

Closed max-out-oluwadara closed 2 weeks ago

max-out-oluwadara commented 1 month ago

Description

Develop an endpoint to handle requests to reset password for registered users. If the password reset is successfully, it will be returned to the client with a '200' status. If an error occurs, an appropriate error status will be returned.

Acceptance Criteria

Purpose

Implement endpoints to facilitate secure password updates for users who have forgotten their passwords or suspect their credentials have been compromised.

Requirement

Expected Outcome

STEP 1

Endpoints

[POST] /api/v1/request-password-request

Description: Initiates the password reset process by verifying the user's email and sending a reset token.

Request

Header Content-Type: application/json

Body

Response

Success (200 OK)

{
    "message": "If a user with that email exists, a password reset link has been sent."
}

Error Response If user cant be found (400 Bad Request)

Server-side error(500 Internal Server Error)


### **STEP 2**

### Endpoints
[POST] /api/v1/reset-password

**Description**: Resets the user's password using the token received via email.

### Request

Headers

Content-Type: application/json X-Reset-Token:


Body
- [ ] json

{ "new_password": "string" }


### Response

_Successful Password Update_
_Status:  `200 OK`_
- [ ]  json

{ "message": "Password updated successfully.", "status_code": 200 }


_Invalid Input or Token_
_Status:  `400 Bad Request`_

{ "message": "Invalid input or token", "status_code": 400 }


_Server-side error_
_Status:  `500 Internal Server Error`_
- [ ] json

{ "message": "Server error during processing.", "status_code": 500, }


### Tests

**Validation and Input Checks:**

- [ ] Verify that all required fields (ewPassword, resetToken) are present in the request body.
- [ ] Ensure that the newPassword meets the defined strength criteria (e.g., minimum length, complexity requirements).

**Token Validation:**
- [ ] Verify that the reset token is valid and not expired.
- [ ] Check that the reset token matches the user’s email.

### Response Scenarios:

**Successful Password Update:**
- [ ] Send a request with a valid reset token and a strong new password.
- [ ] Expect a 200 OK status code.
- [ ] Verify that the response body contains:

{ "message": "Password updated successfully.", "status_code": 200 }


**Invalid Input or Token:**
- [ ] Send a request with missing fields or an invalid reset token.
- [ ] Expect a 400 Bad Request status code.
- [ ] Verify that the response body contains:

{ "message": "Invalid input or token", "status_code": 400 }


**User Not Found:**
- [ ] Send a request with a valid reset token but an email not associated with any user.
- [ ] Expect a 404 Not Found status code.
- [ ] Verify that the response body contains:

{ "message": "User not found", "status_code": 404 }


**Server Error:**
- [ ] Simulate a server error during processing (e.g., database failure).
- [ ] Expect a 500 Internal Server Error status code.
- [ ] Verify that the response body contains:

{ "message": "Server error during processing.", "status_code": 500 }



**Security Measures:**
- [ ] Ensure HTTPS is used for all password reset-related communications.
- [ ] Create unique, time-limited tokens for each password reset request.
- [ ] Enforce strong password policies (length, complexity).
- [ ] Hash the new password using a strong hashing algorithm.
Ayobamidele commented 1 month ago

Tests

Validation and Input Checks:

Token Validation:

Response Scenarios:

{
    "message": "Password updated successfully.",
    "status_code": 200
}
{
    "message": "Invalid input or token",
    "status_code": 400
}
{
    "message": "User not found",
    "status_code": 404
}
{
    "message": "Server error during processing.",
    "status_code": 500
}

Security Measures:

max-out-oluwadara commented 1 month ago

@Ayobamidele thanks, have update the test

phurhard commented 1 month ago

Why you close it initially??

Shullyd7 commented 1 month ago

Why is token in request body instead of request header?

max-out-oluwadara commented 1 month ago

@Shullyd7 Have make the necessary adjustment sir... please help recheck

max-out-oluwadara commented 1 month ago

@phurhard it open now sir.. it a mistake

phurhard commented 1 month ago

A question is asked why, and you just went ahead to change it to headers. Okay, why is the Authtoken in headers?? Is a user signed in before they can reset the password?? Or the token is given to client without signing in??

Ayobamidele commented 1 month ago

It is required for security purposes, so a token must always be generated to verify the process of resetting the password. wherever the user is logged in or not

phurhard commented 1 month ago

I know why a token is needed, I'm asking you if you know why you're putting it in the header or body. The token needed is it the JWT token or it's the confirmation token sent to the user's mail.

Ayobamidele commented 1 month ago

The confirmation token is sent to the user's mail.

phurhard commented 1 month ago

So how would the user send that confirmation mail in the header??

Ayobamidele commented 1 month ago

Corrected sir, It would be in the body.

max-out-oluwadara commented 1 month ago

@phurhard
Have put it into two steps sir

One the user email is verified first. if user is found reset email is sent if user is not found appropriate error response and code is send

two the user click on the link in the email... the link will contain the token and it take the user to the page where the new email will be post to the database. With the approperiate response thanks

max-out-oluwadara commented 1 month ago

@phurhard @Shullyd7 @markessien

The frontend will extract the token sir

headers: { 'Content-Type': 'application/json', 'X-Reset-Token': token }

From the url params example https://example.com/reset-password?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9......