I am authenticating people using passportJS. The problem I have recently realised is the fact that users are able to register for more than one account. I have created a middleware to check whether the email is already in use but somehow it is still passing the test.
var User = require('../models/users');
var authMethods = {};
authMethods.isInUse = function(req,res,next){
User.findOne({"email" : req.body.email}, (err,user) => {
if(user){
req.flash('error',"This mail is already in use.");
res.redirect('/register');
}else {
return next();
}
});
}
module.exports = authMethods;
In my authentication page I am calling the middleware inside the route to meet the condition.
router.post('/register',authMethods.isInUse ,multipart(),function(req, res) {
var image = fs.readFileSync(req.files.image.path);
var profilePic = {data : image, contentType : 'image/png'};
var user = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password,
occupation: req.body.occupation,
phone: req.body.phone,
profilePic : profilePic,
firstName : req.body.firstName,
lastName : req.body.lastName
});
user.save(function(err) {
req.logIn(user, function(err) {
req.flash("success", "Welcome to the site " + user.username);
res.redirect('/flats');
});
});
});
I haven't been able to spot the faulty approach that's causing the problem.
via Ozan
No comments:
Post a Comment