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 // Login user => api/users/login
const loginUser = asyncHandler(async (req, res) => { const loginUser = asyncHandler(async (req, res) => {
const { email, password } = req.body; try {
const { username, password } = req.body;
if(!email || !password) { if(!username || !password) {
return res.status(400).send('Please enter email & password'); return res.status(400).send('Please enter username & password');
} }
const users = await user.findOne({ email }).select('+password'); const users = await user.findOne({ username }).select('+password');
if(!users) { if(!users) {
return res.status(400).send('Invalid Email or Password'); return res.status(400).send('Invalid username or Password');
} }
const isPasswordMatched = await users.comparePassword(password); const isPasswordMatched = await users.comparePassword(password);
if(!isPasswordMatched) { if(!isPasswordMatched) {
return res.status(401).send('Invalid Email or Password'); return res.status(401).send('Invalid username or Password');
} }
sendToken(users, 200, res); sendToken(users, 200, res);
} catch(err){
console.log(err);
}
}); });
// Logout user => api/users/logout // Logout user => api/users/logout