Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Blog-Website/server/controller/user-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ export const singupUser = async (request, response) => {
const hashedPassword = await bcrypt.hash(request.body.password, 10);

const user = { username: request.body.username, name: request.body.name, password: hashedPassword }

//we need to first check if user already present or not if present then we have to throw an error otherwise we have
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move these lines above the password hashing step because if the username already exists, there's no benefit in hashing the password if we're not going to save the user details in the database.

//to save it's details
const check = await User.findOne({username: request.body.username});
if(check){
return response.status(500).json({message:"Username already Used"});
}
const newUser = new User(user);
await newUser.save();

Expand Down Expand Up @@ -55,4 +60,4 @@ export const logoutUser = async (request, response) => {
await Token.deleteOne({ token: token });

response.status(204).json({ msg: 'logout successfull' });
}
}