I am Trying to add login functionality for my Model(User). I have checked my database connection it is working well because other routes are working fine. Like when I create a user it creates a user successfully. So I m sure that there is no certain problem with my database or my database connection. even handfull of my code is working fine.
I m sending the POST request with the following information
{
"email":"sherryijan@gmail.com",
"password":"6421515"
}
app.post("api/users/login", (req, res)=>{
//find email
User.findOne({email: req.body.email}, (err, user) =>{
if(!user) return res.json({
loginSuccess: false,
message: "Auth Failed, email not found"
});
//compare password
user.comparePassword(req.body.password, (err, isMatch) => {
if(!isMatch){
return res.json({loginSuccess:false, message:"wrong password"})
}
})
//generate token
user.generateToken((err, user)=>{
if(err) return res.status(400).send(err);
res.cookie("x_auth", user.token)
.status(200)
.json({
loginSuccess: true
})
})
})
})
the error it shows is
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /api/users/login</pre>
</body>
</html>
I m using postman.