2016-09-29 47 views
0

我想在我的MEAN堆栈应用程序中设置Facebook身份验证。书面作品的代码在Facebook上对用户进行身份验证,但是当它发回我的应用程序时,回调函数会运行,但passport.authenticate('facebook'.....部分不会。如何使用Node.js使用passport.js设置成功的身份验证回调?

这是我的路线文件代码:

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.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); 
       }); 
      } 
      }); 
     }); 
    })); 


} 
+0

添加一些日志记录在您的护照文件?看看它去了哪里? – DrakaSAN

+0

@DrakaSAN它似乎根本不运行护照文件。我已将console.log(x)添加到护照文件的各个部分,并且完全不运行。 – James

回答

0

在你/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); 
}); 
相关问题