fix login

fix login by username to login by email
This commit is contained in:
NekoVari 2023-11-18 16:59:27 +07:00
parent 639b9fa718
commit e534935d80

View file

@ -72,22 +72,22 @@ const updateUser = asyncHandler(async (req, res) => {
// Login user => api/users/login // Login user => api/users/login
const loginUser = asyncHandler(async (req, res) => { const loginUser = asyncHandler(async (req, res) => {
try { try {
const { username, password } = req.body; const { email, password } = req.body;
if(!username || !password) { if(!email || !password) {
return res.status(400).send('Please enter username & password'); return res.status(400).send('Please enter email & password');
} }
const users = await user.findOne({ username }).select('+password'); const users = await user.findOne({ email }).select('+password');
if(!users) { if(!users) {
return res.status(400).send('Invalid username or Password'); return res.status(400).send('Invalid Email');
} }
const isPasswordMatched = await users.comparePassword(password); const isPasswordMatched = await users.comparePassword(password);
if(!isPasswordMatched) { if(!isPasswordMatched) {
return res.status(401).send('Invalid username or Password'); return res.status(401).send('Invalid Password');
} }
sendToken(users, 200, res); sendToken(users, 200, res);