【发布时间】: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