Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passportjs error callback throwing exception

I'm using LocalStrategy with MYSQL(with sequelize) which is working except when MYSQL throws exception (just to test, I shut down MYSQL server). The return done(error) callback throwing exception crashing server.

Here is my piece of code:

passport.use(new LocalStrategy({usernameField: 'email', passwordField: 'password'},
    function (email, password, done) {
        db.User.find({where: {email: email}}).done(function (error, user) {
            if(error) return done(error);

            if (!user) return done(null, false, {message: 'unknown user'});

            //validate password
            if (user.password != password) {
                return done(null, false, {message: 'invalid password'});
            }
            //all ok
            return done(null, user);
        });
    }
));

And the exception:

TypeError: Property 'next' of object #<Context> is not a function
    at Context.actions.error

What am I doing wrong? Thanks!

EDIT:

    req._passport.instance.authenticate('local', function (err, user, info) {
        if (err) return validator.emit('exception', err);

        if (!user) {
            validator.result.errors.push('Username and password combination not found.');
            validator.emit('response');
        } else {
            req.login(user, function (error) {
                if (error) return validator.emit('exception', error);

                validator.emit('response');
            });
        }
    })(req, res);
like image 406
Amitava Avatar asked May 02 '13 11:05

Amitava


People also ask

What is Passportjs?

Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app. The Passport library provides more than 500 authentication mechanisms, including OAuth, JWT, and simple username and password based authentication.

What does passport authenticate() do?

In this route, passport. authenticate() is middleware which will authenticate the request. By default, when authentication succeeds, the req. user property is set to the authenticated user, a login session is established, and the next function in the stack is called.


1 Answers

OK silly me, I missed the next at the end

req._passport.instance.authenticate('local', function (err, user, info) {
.....
})(req, res, next);
like image 149
Amitava Avatar answered Sep 24 '22 01:09

Amitava