【问题标题】:upgrading to express 4 passport stopped working升级到快递4护照停止工作
【发布时间】:2015-01-09 01:12:25
【问题描述】:

我正在升级 express 4,我的护照现在每次都失败。它甚至没有在 passport.use(new LocalStrategy.

中登录控制台

它每次都重定向 /failure 而不会遇到任何断点

// Use the LocalStrategy within Passport.
//   Strategies in passport require a `verify` function, which accept
//   credentials (in this case, a username and password), and invoke a callback
//   with a user object.  In the real world, this would query a database;
//   however, in this example we are using a baked-in set of users.
passport.use(new LocalStrategy(
    function(username, password, done) {
        console.log("LocalStrategy working...");
        // asynchronous verification, for effect...
        process.nextTick(function() {

            // Find the user by username.  If there is no user with the given
            // username, or the password is not correct, set the user to `false` to
            // indicate failure and set a flash message.  Otherwise, return the
            // authenticated `user`.
            findByUsername(username, password, function(err, user) {
                if (err) {
                    return done(err);
                }
                if (!user) {
                    return done(null, false, {
                        message: 'Unknown user ' + username
                    });
                } else {
                    return done(null, user);
                }

            })

        });
    }
));

app.use(cookieParser('keyboard cat'));
app.use(session({
    secret: 'keyboard cat',
    saveUninitialized: true,
    resave: true
}));

// Initialize Passport!  Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());

app.post('/login', passport.authenticate('local', {
        failureRedirect: '/failure',
        failureFlash: false
    }),
    function(req, res) {
        res.cookie('userdata', req.user);
        switch (req.user.role) {
            case 'candidate':
                res.redirect('/app/candidates');
                break;
            case 'employer':
                res.redirect('/app/employers');
                break;
            case 'provider':
                res.redirect('/app/providers');
                break;
            case 'admin':
                res.redirect('/app/admin');
                break;
            default:
                break;
        }
    });

【问题讨论】:

    标签: node.js express passport.js


    【解决方案1】:

    假设您已包含所有相关代码,失败的原因可能是缺少正文解析器。身份验证策略将尝试从req.bodyreq.query 中查找用户名和密码字段,如果没有使用正文解析器,req.body 将为空。然后该策略将 fail straight away 传递给您的验证回调。

    您需要让 Express 应用使用相关的正文解析器,例如:

    var bodyParser = require('body-parser');
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: true}));
    

    【讨论】:

    • 这在我的路线下方,显然必须在护照初始化和会话内容之前调用。谢谢!
    【解决方案2】:

    您是否加入了 LocalStrategy?

    var LocalStrategy   = require('passport-local').Strategy;
    
    app.use(express.cookieParser()); // read cookies 
    app.use(express.bodyParser()); // get information from html forms
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      相关资源
      最近更新 更多