I’m trying to authenticate user. Sign-up is working fine but in sign-in req.body.email is undefined and I can’t figure out why.
Here is my code for sign-in:
router.post('/signin',(res,req,next)=>{
User.findOne({ email: req.body.email })
.exec()
.then(user => {
if(user.length<1){
return res.status(401).json({
message: 'Auth Failed'
})
}
bcrypt.compare(req.body.password, this.user[0].password, (err,result)=>{
if(err){
return res.status(401).json({
message: 'Auth Failed'
});
}
if(result){
return res.status(200).json({
message: 'Auth Successful'
});
}
res.status(401).json({
message: 'Auth Failed'
});
})
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
})
})
On Postman:
{
"email": "[email protected]",
"password": "12345"
}
Console Error:
TypeError: Cannot read properties of undefined (reading 'email')
at C:UsersSJPCauthroutesuser.route.js:62:37
at Layer.handle [as handle_request] (C:UsersSJPCauthnode_modulesexpresslibrouterlayer.js:95:5)
at next (C:UsersSJPCauthnode_modulesexpresslibrouterroute.js:137:13)
at Route.dispatch (C:UsersSJPCauthnode_modulesexpresslibrouterroute.js:112:3)
at Layer.handle [as handle_request] (C:UsersSJPCauthnode_modulesexpresslibrouterlayer.js:95:5)
at C:UsersSJPCauthnode_modulesexpresslibrouterindex.js:281:22
at Function.process_params (C:UsersSJPCauthnode_modulesexpresslibrouterindex.js:341:12)
at next (C:UsersSJPCauthnode_modulesexpresslibrouterindex.js:275:10)
at Function.handle (C:UsersSJPCauthnode_modulesexpresslibrouterindex.js:174:3)
at router (C:UsersSJPCauthnode_modulesexpresslibrouterindex.js:47:12)
I have tried req.query.email but it also thrown the same error.