Skip to content

Commit

Permalink
refactor(auth): Update error status for existing email and username f…
Browse files Browse the repository at this point in the history
…rom 400 to 409

- Changed response status from 400 (Bad Request) to 409 (Conflict) for:
  - "Email already in use" errors during user registration.
  - "Username already in use" errors during user registration.
- Updated corresponding tests to reflect the new 409 status code.
- Updated documentation to specify the correct error codes for email and username conflicts.
  • Loading branch information
TKanX committed Sep 19, 2024
1 parent 233ee05 commit 836102f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
65 changes: 65 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,32 @@ The API uses JWT (JSON Web Token) for authentication. To access protected routes
}
```

- **Status:** `400 Bad Request`

```json
{
"status": "error",
"message": "Invalid password.",
"error": {
"code": "INVALID_PASSWORD",
"details": {}
}
}
```

- **Status:** `400 Bad Request`

```json
{
"status": "error",
"message": "Invalid token.",
"error": {
"code": "INVALID_TOKEN",
"details": {}
}
}
```

- **Status:** `401 Unauthorized`

```json
Expand All @@ -137,6 +163,45 @@ The API uses JWT (JSON Web Token) for authentication. To access protected routes
}
```

- **Status:** `500 Internal Server Error`

```json
{
"status": "error",
"message": "Error verifying token.",
"error": {
"code": "VERIFY_TOKEN_ERROR",
"details": {}
}
}
```

- **Status:** `409 Conflict`

```json
{
"status": "error",
"message": "Email already in use.",
"error": {
"code": "EMAIL_IN_USE",
"details": {}
}
}
```

- **Status:** `409 Conflict`

```json
{
"status": "error",
"message": "Username already in use.",
"error": {
"code": "USERNAME_IN_USE",
"details": {}
}
}
```

- **Status:** `500 Internal Server Error`

```json
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ const completeRegistration = async (req, res) => {
// Check if the email is already in use
const existingEmail = await userService.getUserByEmail(email);
if (existingEmail) {
return res.badRequest("Email already in use.", "EMAIL_IN_USE");
return res.conflict("Email already in use.", "EMAIL_IN_USE");
}

// Check if the username is already in use
const existingUsername = await userService.getUserByUsername(username);
if (existingUsername) {
return res.badRequest("Username already in use.", "USERNAME_IN_USE");
return res.conflict("Username already in use.", "USERNAME_IN_USE");
}

// Create the user
Expand Down
8 changes: 4 additions & 4 deletions tests/controllers/authController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe("AuthController - completeRegistration", () => {
expect(res.body.error.code).toBe("INVALID_PASSWORD");
});

it("should return 400 if email is already in use", async () => {
it("should return 409 if email is already in use", async () => {
jwtService.verifyToken.mockReturnValue({ email: "test@example.com" });
validationUtils.validateUsername.mockReturnValue(true);
validationUtils.validatePassword.mockReturnValue(true);
Expand All @@ -148,12 +148,12 @@ describe("AuthController - completeRegistration", () => {
password: "Password123!",
});

expect(res.status).toBe(400);
expect(res.status).toBe(409);
expect(res.body.message).toBe("Email already in use.");
expect(res.body.error.code).toBe("EMAIL_IN_USE");
});

it("should return 400 if username is already in use", async () => {
it("should return 409 if username is already in use", async () => {
jwtService.verifyToken.mockReturnValue({ email: "test@example.com" });
validationUtils.validateUsername.mockReturnValue(true);
validationUtils.validatePassword.mockReturnValue(true);
Expand All @@ -168,7 +168,7 @@ describe("AuthController - completeRegistration", () => {
password: "Password123!",
});

expect(res.status).toBe(400);
expect(res.status).toBe(409);
expect(res.body.message).toBe("Username already in use.");
expect(res.body.error.code).toBe("USERNAME_IN_USE");
});
Expand Down

0 comments on commit 836102f

Please sign in to comment.