使用Passport无法使用Facebook进行身份validation

我正在尝试使用我的应用程序进行Facebook身份validation。 但到目前为止,我的尝试是不成功的。 我对Node完全陌生。

当用户点击那个fbloginbutton时,请求被带到/ auth / facebook路由,在那里他们将被传递到Passport策略。 在那里,他们将被发送到Facebook进行身份validation。 但它从来没有发生。 Facebookauthentication窗口永远不会显示。 好像redirect不起作用。 我做了几个小时的search,但没有得到解决scheme。

在这里输入图像描述

以下是我认为对这种情况很重要的部分代码

我已经把我的应用程序ID和应用程序的秘密在config / auth.js

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

使用Facebook进行身份validation并处理callback的策略是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('/'); }); }; 

loginbutton代码在视图中

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

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

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

  1. 点击“+添加平台”,select“网站”
  2. 在刚刚添加的网站框中:网站url: http://localhost:8080/
  3. 在上面的那个框(设置=>基本):应用程序域:本地主机
  4. 在右下angular – 点击“保存更改”

一旦我完成了,我的问题就解决了。

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

另外,上面应该改成:

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