最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - passport.authenticate callback not being executed - Stack Overflow

programmeradmin4浏览0评论

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 ?

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 ?

Share Improve this question edited Jul 23, 2022 at 20:48 ggorlen 56.9k8 gold badges110 silver badges150 bronze badges asked Nov 6, 2017 at 15:27 Mohamed EttayebMohamed Ettayeb 1591 gold badge2 silver badges10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 20

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)
})

When you use passport.authenticate as a callback, it is your application's responsibility to establish a session (by calling req.login()) and send a response.

Here is link to passport.js documentation that could help: http://www.passportjs.org/docs/authenticate/

发布评论

评论列表(0)

  1. 暂无评论