【问题标题】:passport node authenticate success护照节点认证成功
【发布时间】:2014-12-12 06:07:51
【问题描述】:

我正在努力在我的节点应用程序中实现护照身份验证,但我无法理解为什么需要重定向才能访问响应 (res) 属性?

app.get('/api/loginFailure', function(req, res) {
    res.status(401).json({message: 'Login Failed', success: true});
});

app.get('/api/loginSuccess', function(req, res) {
    res.status(200).json({message:'Welcome!', success: true});

});


// process the login form
app.post('/api/login', passport.authenticate('local-login', { 
    successRedirect: '/api/loginSuccess',
    failureRedirect: '/api/loginFailure'}));

如您所见,我使用 successRedirect 访问不同的路由,以便发回 json 响应。我不希望节点 api 重定向实际应用程序,因为其目的是使其与前端无关。

本地登录策略如下。我怀疑我的困难可能在于我如何从方法中返回;

    passport.use('local-login', new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField: 'email',
        passwordField: 'password',
        passReqToCallback: true // allows us to pass back the entire request to the callback
    },

    function(req, email, password, done) { // callback with email and password from our form

        // find a user whose email is the same as the forms email
        // we are checking to see if the user trying to login already exists
        User.findOne({
                'local.email': email
            },

            function(err, user) {
                // if there are any errors, return the error before anything else
                if (err)
                    return done(err);

                // if no user is found, return the message
                if (!user) {
                    return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
                }

                // if the user is found but the password is wrong
                if (!user.validPassword(password)) {
                    return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
                }

                // all is well, return successful user
                return done(null, user);
            });

    }));

我打算删除所有的 flashdata 和什么,但现在只要能够将 2 个额外的 api 路由折叠到 /api/login 中就很好了。

【问题讨论】:

    标签: node.js passport.js


    【解决方案1】:

    我无法理解为什么需要重定向才能访问响应 (res) 属性?

    如果您检查了 passport documentation,而不是从 this guide 复制您的代码(用于另一种用途),您会发现它并不总是需要重定向。

    您也可以通过以下方式使用它:

    app.post('/login',
      passport.authenticate('local'),
      function(req, res) {
        // If this function gets called, authentication was successful.
        // `req.user` contains the authenticated user.
        res.redirect('/users/' + req.user.username);
      }
    );
    

    【讨论】:

    • 这就是指南的目的吗?起点?我知道我可以做你上面强调的事情,我还不明白如何从自定义策略中返回,以便可以应用上述相同的逻辑。
    • 您已经尝试使用相同的passport.use() 块?
    • 我试过 app.post('/login', passport.authenticate('local-login'), function(req, res) {});但属性 res 不可用。这就是为什么我认为我的本地策略中的 return 语句是不正确的。
    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2019-03-01
    • 1970-01-01
    相关资源
    最近更新 更多