Express PassportRedirect error not working

I have set a strategy local, but failureRedirectit is not working properly. When a connection error occurs (for example, the database URL is incorrect), the response is a 500 error instead of being redirected to the specified route.

This is my route code:

router.route('/login')
    .post(passport.authenticate('local', {
        failureRedirect: '/' 
    }), function(req, res){ 
        console.log('user logged in');
        res.redirect('../console');
    });

And here is my implementation for the strategy local:

module.exports = function(){
    passport.use(new LocalStrategy({
        usernameField: 'email',
        passwordField: 'password'
    },
        function(email, password, done){
            pg.defaults.ssl = true;
            pg.connect(process.env.DATABASE_URL, function(err, client) {
                if (err){
                    console.log('Connection issue when logging in: ' + JSON.stringify(err));
                    done('Error with database,', null); // this is the problem area!!!
                } else {
                    client
                        .query(`SELECT * FROM agent WHERE email='${email}'`, function(err, result) {
                            if(err || result.rows.length === 0 ) {
                                console.log('Query issue when loggin in: '+ JSON.stringify(err));
                                done(null, false);
                            } else {
                                var user = result;
                                console.log('ready to log user in');
                                done(null, user);
                            }
                        });
                }
            });
        }
    ));
};

I thought my use of the callback function was done()wrong, but I was following the documentation. Thank you for your help.

+4
source share
2 answers

You should

throw new Error ('hello world')

,

https://docs.nodejitsu.com/articles/errors/what-is-try-catch/

-1

, , - done('some error', null);, , Passport.

. , done(null, null), .

" " . null.

+1

All Articles