Search code examples
javascriptnode.jsexpresspassport.js

passport.authenticate callback not being executed


I m developing a backend with nodejs v8.7.0, for the authentication im using passport and local passport , i worked with this before and everything going well , but now i dont know what is the problem, well this is my code :

my strategy :

var passport = require('passport')
var LocalStrategy = require('passport-local').Strategy
    passport.use('local', new LocalStrategy({
            usernameField: 'email'
        },
        function (email, password, done) {
            User.findOne({
                email: email,
                is_admin: false
            }, function (err, user) {
                if (err) {
                    return done(err)
                }
                // Return if user not found in database
                if (!user) {
                    return done(null, false, {
                        message: 'User not found'
                    })
                }
                // Return if password is wrong
                if (!user.validPassword(password)) {
                    return done(null, false, {
                        message: 'Password is wrong'
                    })
                }
                // If credentials are correct, return the user object
                return done(null, user)
            })
        }
    ))

and here is where the call happen :

router.post('/login', (req, res) => {
    try {
        passport.authenticate('local', function (err, user, info) {
            console.log('here xx')

            var token;
            if (err) {
                console.log(err);
                return res.status(401).json(err);
            }
            // If a user is found
            if (user) {
                console.log('here')

                token = user.generateJwt();
                return res.status(200).json({
                    "token": token
                });
            } else {
                // If user is not found
                res.status(401).json(info);
            }
        })

    } catch (reason) {
        res.status(501).json({
            message: reason.message
        })
    }
})

the problem is that im getting nothing like the message from console.log or anything like errors, its not executing the callback from passport.authenticate.... by the way everything like registering new users and everything is working well anyone can help please ?


Solution

  • Passport should be used as middleware. Use it like this in your route:

    router.post('/login', passport.authenticate('local'), (req, res) => { })

    Inside your route, you'll have access to req.user

    If you're not using as middleware, you can use a callback. But you need to pass in req, res and next.

    For example:

    passport.authenticate('local', function (err, user, info) {
    
    })(req, res, next)
    

    Here's a full example, note this is not using middleware as in this case you're defining custom messages and need to use a callback to get the info.

    router.post('/login', (req, res) => {
        passport.authenticate('local', function (err, user, info) {      
            if (err) {
                return res.status(401).json(err);
            }
            if (user) {
                const token = user.generateJwt();
                return res.status(200).json({
                    "token": token
                });
            } else {
                res.status(401).json(info);
            }
        })(req, res, next)
    })