Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
62
rated 0 times [  63] [ 1]  / answers: 1 / hits: 18903  / 7 Years ago, thu, june 1, 2017, 12:00:00

I'm using passport.js for the authentication and handling of sessions in my project.



I use email to authenticate from passport but when the done is called on verification, I get the error done is not a function.



at Promise.<anonymous> (/Users/sultan/Desktop/too2/controllers/users.js:39:28)
at Promise.<anonymous> (/Users/sultan/Desktop/too2/node_modules/mongoose/lib/promise.js:120:8)


Code



  // LOCAL LOGIN =============================================================
passport.use('local-login', new LocalStrategy({
usernameField : 'email',
passwordField : 'password'
//passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function( email, password, done) {
if (email)
email = email.toString().toLowerCase();

// asynchronous
process.nextTick(function() {
User.findOne({ email: email }, function(err, user) {
// if there are any errors, return the error
if (err)
return done(err);

// if no user is found, return the message
if (!user)
//(/Users/sultan/Desktop/too2/controllers/users.js:39:28)
return done(null, false, {message: 'No user found.'});

if (!user.validPassword(password))
return done(null, false, {message: 'Oops! Wrong Password'});

// all is well, return user
else
return done(null, user);
});
});

}));


What is the cause of this error and how do I resolve it?


More From » node.js

 Answers
3

this worked for me



passport.use('login', new LocalStrategy({
// by default, local strategy uses username
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done) {
if (email) email = email.toLowerCase();

// asynchronous
process.nextTick(function() {
User.findOne({ 'email' : email }, function(err, user) {
// if there are any errors, return the error
if (err)
return done(err);

// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.'));

if (!user.validPassword(password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));

// all is well, return user
else
return done(null, user);
});
});

}));

[#57588] Wednesday, May 31, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
domeniccolti

Total Points: 276
Total Questions: 98
Total Answers: 93

Location: India
Member since Fri, May 13, 2022
2 Years ago
domeniccolti questions
Mon, Oct 18, 21, 00:00, 3 Years ago
Thu, Oct 14, 21, 00:00, 3 Years ago
Thu, Jul 15, 21, 00:00, 3 Years ago
Sat, Oct 24, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;