Skip to content

Commit

Permalink
feat-#301: Removed extra db call
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmohitsen committed May 19, 2024
1 parent c65df71 commit 32feccd
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions backend/controllers/auth-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ export const signUpWithEmail = asyncHandler(async (req, res) => {
throw new ApiError(HTTP_STATUS.BAD_REQUEST, RESPONSE_MESSAGES.COMMON.REQUIRED_FIELDS)
}

const existingUserByUserName= await User.findOne({ userName });
if(existingUserByUserName){
throw new ApiError(HTTP_STATUS.BAD_REQUEST, RESPONSE_MESSAGES.USERS.USER_USERNAME_EXISTS)
}
const existingUser = await User.findOne({
$or: [{ userName }, { email }]
});

const existingUserByEmail = await User.findOne({ email });
if(existingUserByEmail){
throw new ApiError(HTTP_STATUS.BAD_REQUEST, RESPONSE_MESSAGES.USERS.USER_EMAIL_EXISTS)
if (existingUser) {
if (existingUser.userName === userName) {
throw new ApiError(HTTP_STATUS.BAD_REQUEST, RESPONSE_MESSAGES.USERS.USER_USERNAME_EXISTS);
}
if (existingUser.email === email) {
throw new ApiError(HTTP_STATUS.BAD_REQUEST, RESPONSE_MESSAGES.USERS.USER_EMAIL_EXISTS);
}
}

const user = new User({
Expand Down

0 comments on commit 32feccd

Please sign in to comment.