I am getting an undefined function error when trying to log a user in after creating their account via Mongoose…
TypeError: undefined is not a function
at /node_modules/passport/lib/passport/http/request.js:44:48
at pass (/node_modules/passport/lib/passport/index.js:240:14)
at Passport.serializeUser (/node_modules/passport/lib/passport/index.js:242:5)
at IncomingMessage.req.login.req.logIn (/node_modules/passport/lib/passport/http/request.js:43:29)
at Promise.<anonymous> (/routes/index.js:33:25)
at Promise.addBack (/node_modules/mongoose/lib/promise.js:120:8)
My register function in routes/index.js looks like this:
exports.register = function (req, res) {
// Generate salt
Common.bcrypt.genSalt(function(err, salt) {
req.body.salt = salt;
// Generate hash
Common.bcrypt.hash(req.body.password, salt, function(error, hash) {
req.body.hash = hash;
// Remove clear text password
delete req.body.password;
// Save new user
new Model.User(req.body).save(function(err, user) {
console.log(err);
console.log(req);
if (user) {
req.login(user);
req.redirect('/');
} else {
res.json(false);
}
});
});
});
Any ideas?
If you have a look at
node_modules/passport/lib/passport/http/request.js, line 44 column 48 you will see that the functiondoneis being invoked. The error tells you thatundefined is not a function, sodoneis not defined. If you look on line 29 of the same file, you will see thatdoneis a parameter you are supposed to pass to thereq.loginfunction. In other words, it’s expecting a callback and you are not providing one. Maybe something like this:While I use passport, I don’t call
req.logindirectly so I’m not 100% certain on the implications of the code above. I’ll also point out that I do all of the bcrypt stuff in my user model, which seems like a cleaner way to approach that aspect of the problem.