我想在我的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);
});
}
});
});
}));
}
添加一些日志记录在您的护照文件?看看它去了哪里? – DrakaSAN
@DrakaSAN它似乎根本不运行护照文件。我已将console.log(x)添加到护照文件的各个部分,并且完全不运行。 – James