config loginHandler

This commit is contained in:
NekoVari 2023-09-27 00:47:06 +07:00
parent edc2231487
commit 1a7b7e4333

View file

@ -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