有一些问题要弄清楚如何通过我的Facebook登录访问当前用户。我使用的是passportJS,Node,express。我认为我的用户'没有登录,但我无法检查。我会上传我所拥有的内容并感谢任何关注它的人 - 真的很感激。
route.js
gameplay()
passport.js
app.get('/auth/facebook', passport.authenticate('facebook', { scope : ['email', 'public_profile', 'user_friends'] }));
// 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('/');
});
};
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/');
}
server.js
passport.use(new FacebookStrategy({
// pull in our app id and secret from our auth.js file
clientID : configAuth.facebookAuth.clientID,
clientSecret : configAuth.facebookAuth.clientSecret,
callbackURL : configAuth.facebookAuth.callbackURL,
// profileFields: ['id', 'name','picture.type(large)', 'emails', 'username', 'displayName', 'about', 'gender']
},
// facebook will send back the token and profile
function(token, refreshToken, profile, done) {
// asynchronous
process.nextTick(function() {
// find the user in the database based on their facebook id
User.findOne({ 'facebook.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.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
console.log(profile);
console.log(user);
console.log('it is working');
// save our user to the database
newUser.save(function(err) {
if (err)
throw err;
// if successful, return the new user
return done(null, newUser);
});
}
});
});
})); // end of FacebookStrategy
};
这是我的第一篇stackoverflow帖子,所以如果我侮辱了任何格式的人,请高级道歉。
答案 0 :(得分:1)
您的用户应该如何序列化。例如:
// set up cookie parser and session
var cookieParser = require('cookie-parser');
var session = require('express-session');
app.use(cookieParser());
app.use(session({
secret: 'mysecret',
resave: true,
saveUninitialized: false
}));
// passport init
app.use(passport.initialize());
app.use(passport.session());
// Lets user information be stored and retrieved from session
passport.serializeUser(function(user, done) {
done(null, user.facebook.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err,user){
if(err) done(err);
done(null,user);
});
});
然后,您可以通过req.user
访问用户对象。例如,测试路线可以是:
app.get('/user', function(req, res, next) {
res.send(req.user);
});
祝你好运!
答案 1 :(得分:0)
你也可以用另一种方式来做:
router.get('/auth/facebook', function(req, res, next) {
passport.authenticate('facebook', { scope : ['email', 'public_profile', 'user_friends'] } , function(err, user, info) {
if(err) return res.status(400).send(err);
if(user._id){
req.logIn(user, function(err) {
if (err) { return next(err); }
//redirect where you want
return res.redirect("");
});
}
})(req, res, next);
})
req.logIn 是用户obj创建会话和维护所需的功能。否则护照永远不会维持用户的会话。