【问题标题】:How do I set up a successful authentication callback with passport.js using Node.js?如何使用 Node.js 通过 passport.js 设置成功的身份验证回调?
【发布时间】:2016-10-05 18:18:52
【问题描述】:

我正在尝试在我的 MEAN 堆栈应用程序中设置 facebook 身份验证。编写的代码可以在 facebook 上对用户进行身份验证,但是当它被发送回我的应用程序时,回调函数运行但 passport.authenticate('facebook'..... 部分没有。

这是我在路由文件中的代码:

    app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));

    app.get('/auth/facebook/callback', function() {
        console.log('callback')
         passport.authenticate('facebook', {
            successRedirect : '/',
            failureRedirect : '/fail'
          })
    });

“回调”在控制台中输出,但应用程序只是处于加载状态,直到超时。

这是我用来配置passport的passport.js文件:

// config/passport.js



// load all the things we need
var LocalStrategy    = require('passport-local').Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;

// load up the user model
var User       = require('../app/models/user');

// load the auth variables
var configAuth = require('./auth');


module.exports = function(passport) {


    passport.use('facebook', new FacebookStrategy({
        clientID        : configAuth.facebookAuth.clientID,
        clientSecret    : configAuth.facebookAuth.clientSecret,
        callbackURL     : configAuth.facebookAuth.callbackURL,
        profileFields: ["emails", "displayName"]
    },

      // facebook will send back the tokens and profile
      function(access_token, refresh_token, profile, done) {
        // asynchronous
        process.nextTick(function() {

          // find the user in the database based on their facebook id
          User.findOne({ 'id' : profile.id }, function(err, user) {

            // if there is an error, stop everything and return that
            // ie an error connecting to the database
            if (err)
              return done(err);

              // if the user is found, then log them in
              if (user) {
                return done(null, user); // user found, return that user
              } else {
                // if there is no user found with that facebook id, create them
                var newUser = new User();

                // set all of the facebook information in our user model
                newUser.fb.id    = profile.id; // set the users facebook id                 
                newUser.fb.access_token = access_token; // we will save the token that facebook provides to the user                    
                newUser.fb.firstName  = profile.name.givenName;
                newUser.fb.lastName = profile.name.familyName; // look at the passport user profile to see how names are returned
                newUser.fb.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first

                // save our user to the database
                newUser.save(function(err) {
                  if (err)
                    throw err;

                  // if successful, return the new user
                  return done(null, newUser);
                });
             } 
          });
        });
    }));


}

【问题讨论】:

  • 在您的护照文件中添加一些日志?看看它去哪儿了?
  • @DrakaSAN 似乎根本没有运行护照文件。我已将 console.log(x) 添加到护照文件的各个部分,但它根本没有运行。

标签: node.js facebook authentication passport.js


【解决方案1】:

在您的/auth/facebook/callback 中,您正在路由处理程序中执行passport.authenticate,而不是作为中间件,即。文档中称为custom callback 的内容。此调用返回一个函数,但您没有执行它,所以什么也没有发生。

您需要在此处将passport.authenticate 作为中间件运行(即the usual way),或者实际使用您的路由处理程序接收的reqresnext 参数(但您省略了)并将它们传递给路由处理程序中的authenticate 调用。

中间件方式如documentation of passport-facebook所示,即。对你来说是:

app.get('/auth/facebook/callback',
    passport.authenticate('facebook', {
        successRedirect : '/',
        failureRedirect : '/fail'
    })
);

如果您出于某种原因希望使用自定义回调,则需要如下所示:

app.get('/auth/facebook/callback', function(req, res, next) {
    passport.authenticate('facebook', function(err, user, info) {
        // Do your things and then call `req.logIn` and stuff
    })(req, res, next);
});

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2020-07-20
    • 2013-06-26
    • 2020-05-19
    • 2017-08-20
    • 1970-01-01
    • 2017-06-27
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多