完成不是passport.js中的功能

时间:2019-05-28 09:46:18

标签: node.js express passport.js

const passport = require('passport');
const localStrategy = require('passport-local').Strategy;
const mongoose = require('mongoose');

var User = mongoose.model('User');

passport.use(
    new localStrategy({ usernameField: 'email' },
        (req, username, password, done) => {
            User.findOne({ email: username },
                (err, user) => {

                    if (err)
                        return done(err);
                    // unknown user
                    else if (!user)
                        return done(null, false, { message: 'Email is not registered' });
                    // wrong password
                    else if (!user.verifyPassword(password))
                        return done(null, false, { message: 'Wrong password.' });
                    // wrong type
                    else if (!user.verifyType(req.body.type))
                        return done(null, false, { message: 'Wrong type.' });
                    // authentication succeeded
                    else
                        return done(null, user);
                });
        })
);

完成不是函数是错误 使用PassportJS,如何将一个额外的表单字段传递给本地身份验证策略?

2 个答案:

答案 0 :(得分:0)

只需在新的localStrategy内部的匿名函数中删除“ req”参数,就可以了。

此代码中的任何地方都没有req参数,因此您无法在其中使用它

像这样:

 (username, password, done) => {

答案 1 :(得分:0)

passport.use(
    new localStrategy({ usernameField: 'email' },
        yourParameter
        (req, username, password, done) => {
            User.findOne({ email: username },
                (err, user) => {

                    if (err)
                        return done(err);
                    // unknown user
                    else if (!user)
                        return done(null, false, { message: 'Email is not registered' });
                    // wrong password
                    else if (!user.verifyPassword(password))
                        return done(null, false, { message: 'Wrong password.' });
                    // wrong type
                    else if (!user.verifyType(yourParameter))
                        return done(null, false, { message: 'Wrong type.' });
                    // authentication succeeded
                    else
                        return done(null, user);
                });
        })
);