TypeError: done is not a function and TypeError: assert not a function

Issue

I am doing a mean stack app, and right now I am working on registering and sign in authentication. But I am having two errors!
If I have”passReqToCallback: true” it gives me “done is not a function” but when I comment it out it is fine. But don’t I need that?

After that it tells me that assert is not a function, then I tried to switch it with check, and it gave me that check isn’t a function.
I am so confused why It is giving these errors

var passport = require('passport');
var  User = require('../models/User');
var LocalStrategy = require('passport-local').Strategy;
var validator = require('express-validator');


passport.serializeUser(function(user , done ){
done(null, user.id);
});
passport.deserializeUser(function(id, done){
User.findById(id,function(err, user){
    done(err, user);
});
});

passport.use('local.register', new LocalStrategy({
usernameField: 'username',
emailField: 'email',
passwordField: 'password',
passReqToCallback : true
}, function(req, username, email, password, done){
req.assert('username', 'Invalid username').notEmpty();
req.assert('email', 'Invalid email').notEmpty().isEmail();
req.assert('password', 'Invalid password').notEmpty().isLength({min: 4});

var errors = req.validationErrors();


if(errors){
var messages = [];
errors.forEach(function(error){
messages.push(error.msg);
});
return done(null, false, req.flash('error', messages));
}
User.findOne({'username':username}, function(err, user){
if(err){
return done(err);
}
if(user){
return done(null, false, {message:'Username is already use'});
}

var newUser = new User();
newUser.username = username;
newUser.email = email;
newUser.password = newUser.encryptPassword(password);
newUser.save(function(err, result) {
if (err) {
return done(err);
}
return done(null , newUser);
});
});
}));
passport.use('local.sign-in', new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true
}, function(req, username, password, done){
req.assert('username', 'Invalid username').notEmpty();
req.assert('password', 'Invalid password').notEmpty();
var errors = req.validationErrors();
if(errors){
var messages = [];
errors.forEach(function(error){
messages.push(error.msg);
});
return done(null, false, req.flash('error', messages));
}
//find user
User.findOne({'username': username}, function(err, user){
if(err){
return done(err);
}

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

     }));

Solution

According to https://github.com/jaredhanson/passport-local#available-options the only options are usernameField and passwordField, and the callback only accepts username, password, done as arguments if passReqToCallback is false, and req, username, password, done if it’s true. The email parameter in the callback shouldn’t be there.

To be able to use req.assert, you need to have access to the request object, so, you need to pass passReqToCallback: true.

It should be something like this:

passport.use('local.register', new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password',
    passReqToCallback : true
}, function(req, username, password, done) {
    req.assert ...
});

This should fix both of your problems.

Answered By – luisenrike

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published