【问题标题】:Authentication using Passport JS for facebook使用 Passport JS for facebook 进行身份验证
【发布时间】:2018-02-04 06:48:54
【问题描述】:

点击以下链接后,不会重定向到 facebook.com:

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

路线如下:

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

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

策略代码如下:

var FacebookStrategy = require('passport-facebook').Strategy;
passport.use(new FacebookStrategy({
        clientID: configAuth.facebookAuth.clientID,
        clientSecret: configAuth.facebookAuth.clientsecret,
        profileFields: ['id', 'displayName', 'email'],
        callbackURL: configAuth.facebookAuth.callbackURL
    },
    function(accessToken, refreshToken, profile, done) {
        process.nextTick(function() {
            User.findOne({
                'facebook.id': profile.id
            }, function(err, user) {
                if (err)
                    return done(err);
                if (user)
                    return done(null, user);
                else {
                    var newUser = new User();
                    newUser.facebook.id = profile.id;
                    newUser.facebook.token = accessToken;
                    newUser.facebook.name = profile.name.givenName + ' ' + profile.name.familyName;
                    newUser.facebook.email = profile.email[0].value;

                    newUser.save(function(err) {
                        if (err)
                            throw err;
                        else
                            return done(null, newUser);
                    })
                }
            });
        });

    }
));

我已经更新了我的 auth.js 文件中的秘密。 我还更新了网站 URL:http://localhost:8080 并在 developers.facebook.com 中添加了 appdomain : localhost

每当我点击 HTML 链接时,控件都会停留在同一页面上,然后会显示网页 http://localhost:8080/auth/facebook 找不到

【问题讨论】:

    标签: node.js facebook oauth passport.js


    【解决方案1】:

    您应该将 facebook 开发人员中的网站 url 更新为 http://localhost:8080/auth/facebook/callback。据我所知,您的代码没有任何问题。我也创建了这个应用程序,你的代码和我的代码之间的唯一区别是我使用了 ssl 证书。即使更新后的网址对您不起作用,也请告诉我。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 2015-03-16
    • 2017-03-16
    • 2015-03-24
    • 1970-01-01
    相关资源
    最近更新 更多