【问题标题】:Authenticating With Facebook using Passport Not Working使用 Passport 通过 Facebook 进行身份验证不起作用
【发布时间】:2014-10-18 03:34:39
【问题描述】:

我正在尝试对我的应用程序进行 Facebook 身份验证。但到目前为止,我的尝试都没有成功。我对 Node 完全陌生。

当用户单击该 fb 登录按钮时,需要将请求带到 /auth/facebook 路由,在那里它们将被传递到 Passport 策略。在那里,它们将被发送到 Facebook 进行身份验证。但它永远不会发生。 Facebook 身份验证窗口永远不会显示。似乎重定向不起作用。我进行了几个小时的搜索,但没有得到解决方案。

以下是我认为对上下文很重要的代码部分

我已将我的 App ID 和 App Secret 放入 config/auth.js

module.exports = {

    'facebookAuth' : {
        'clientID'      : 'my-AppID-here', 
        'clientSecret'  : 'my-App-secret-here', 
        'callbackURL'   : 'http://localhost:8080/auth/facebook/callback'
    }
};

通过 Facebook 进行身份验证和处理回调的策略在 config/passport.js

    var LocalStrategy    = require('passport-local').Strategy;
    var FacebookStrategy = require('passport-facebook').Strategy;

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

    module.exports = function(passport) {

        passport.serializeUser(function(user, done) {
            done(null, user.id);
        });

        passport.deserializeUser(function(id, done) {
            User.findById(id, function(err, user) {
                done(err, user);
            });
        });    

        // Facebook Strategy
        passport.use(new FacebookStrategy({

            // pull app id and secret from our auth.js file
            clientID        : configAuth.facebookAuth.clientID,
            clientSecret    : configAuth.facebookAuth.clientSecret,
            callbackURL     : configAuth.facebookAuth.callbackURL

        },

        // facebook will send back the token and profile
        function(token, refreshToken, profile, done) {

            process.nextTick(function() {

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

                    if (err)
                        return done(err);

                    // if the user is found, then log them in
                    if (user) {
                        return done(null, 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.facebook.id    = profile.id; // set the users facebook id                   
                        newUser.facebook.token = token; // we will save the token that facebook provides to the user                    
                        newUser.facebook.name  = profile.name.givenName + ' ' + profile.name.familyName; // look at the passport user profile to see how names are returned
                        newUser.facebook.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);
                        });
                    }

                });
            });

        }));

    };

app/routes.js 中的相关路由

    module.exports = function(app, passport) {

        // route for facebook authentication and login
        app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));

        // handle the callback after facebook has authenticated the user
        app.get('/auth/facebook/callback',
            passport.authenticate('facebook', {
                successRedirect : '/profile',
                failureRedirect : '/'
            }));

        // route for logging out
        app.get('/logout', function(req, res) {
            req.logout();
            res.redirect('/');
        });

    };

视图中的登录按钮代码

<a href="/auth/facebook" class="btn btn-primary"><span class="fa fa-facebook"></span> Facebook</a>

【问题讨论】:

    标签: node.js facebook authentication express passport.js


    【解决方案1】:
     app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));
    

    另外,上面应该改为:

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

    【讨论】:

      【解决方案2】:

      问题是我没有在 Facebook 应用设置中附加域名(在我的情况下是 http://localhost:8080)。

      转到“基本”选项卡下的“设置”页面

      1. 点击“+添加平台”,选择“网站”
      2. 在您刚刚添加的网站出现的框中: 网站 URL:http://localhost:8080/
      3. 在上面的框中(设置 => 基本):应用域:本地主机
      4. 在右下角 - 点击“保存更改”

      一旦我这样做了,我的问题就解决了。

      【讨论】:

      • 这是 FB API 恕我直言的一个错误,因为这个 url (localhost) 被发布为任何上传照片的超链接。我花了将近一周的时间试图解决因此无法退出的问题!
      猜你喜欢
      • 2018-02-04
      • 2016-07-19
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多