diff --git a/controllers/userController.js b/controllers/userController.js index 0e300a5..6004b7d 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -71,25 +71,29 @@ const updateUser = asyncHandler(async (req, res) => { // Login user => api/users/login const loginUser = asyncHandler(async (req, res) => { - const { email, password } = req.body; + try { + const { username, password } = req.body; - if(!email || !password) { - return res.status(400).send('Please enter email & password'); + if(!username || !password) { + return res.status(400).send('Please enter username & password'); + } + + const users = await user.findOne({ username }).select('+password'); + + if(!users) { + return res.status(400).send('Invalid username or Password'); + } + + const isPasswordMatched = await users.comparePassword(password); + + if(!isPasswordMatched) { + return res.status(401).send('Invalid username or Password'); + } + + sendToken(users, 200, res); + } catch(err){ + console.log(err); } - - const users = await user.findOne({ email }).select('+password'); - - if(!users) { - return res.status(400).send('Invalid Email or Password'); - } - - const isPasswordMatched = await users.comparePassword(password); - - if(!isPasswordMatched) { - return res.status(401).send('Invalid Email or Password'); - } - - sendToken(users, 200, res); }); // Logout user => api/users/logout