i'm trying to get passport to work with my node express server. I can login in with Facebook and find the correct user in my database; however, when I redirect req.user
is always undefined. Here is my server code:
var express = require('express'),
path = require('path'),
http = require('http'),
passport = require('passport'),
FacebookStrategy = require('passport-facebook').Strategy,
user = require('./routes/users');
var app = express();
passport.use(new FacebookStrategy({
clientID: "HIDDEN",
clientSecret: "HIDDEN",
callbackURL: "http://MYURL.com/auth/facebook/callback",
passReqToCallback: true
},
function(req, accessToken, refreshToken, profile, done) {
user.findOrCreate(profile, function(err, user) {
if (err) { return done(err); }
done(null, user);
});
}
));
passport.serializeUser(function(user, done) {
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
user.findById(id, function(err, user) {
done(err, user);
});
});
app.configure(function () {
app.set('port', process.env.PORT || 3000);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.logger('dev')); /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: 'foobar' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
app.get('/auth/user', function (req, res) {
console.log(req);
});
app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/',
failureRedirect: '/login' }));
http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
I go to /auth/facebook, it finds the correct user and redirects me to /. But then I go to /auth/user and req.user
is undefined and my sessions show this:
cookies: { 'connect.sid': 's:JFdQkihKQQyR4q70q7h2zWFt.VS+te0pT0z/Gtwg7w5B33naCvA/ckKMk60SFenObxUU' },
signedCookies: {},
url: '/auth/user',
method: 'GET',
sessionStore:
{ sessions:
{ '5sk3Txa2vs5sYhvtdYwGaUZx': '{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"passport":{"user":"50c527c9c6cb41860b000001"}}',
'Au6m0hAj/3warKOGNSWw0yu2': '{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"passport":{}}',
JFdQkihKQQyR4q70q7h2zWFt: '{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"passport":{}}' },
generate: [Function],
_events: { disconnect: [Function], connect: [Function] } },
sessionID: 'JFdQkihKQQyR4q70q7h2zWFt',
Does it have something to do with my sessionID not matching the session where the passport user is set?
Update
So I determined that the sessionID not matching was because I'm running my code on c9.io and it actually has two URLs. When I use the correct URL and go to /auth/user my sessionID matches the session with passport user set and I can see in the log my deserializeUser finding the correct user object. However, req.user
is still undefined after this.
Trying to find user with id: 50c527c9c6cb41860b000001
{ sessions:
{ yoOUOxyXZ0SmutA0t5xUr6nI: '{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"passport":{"user":"50c527c9c6cb41860b000001"}}',
goZpmK3y3tOfn660hRbz2hSa: '{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"passport":{}}',
'O5Sz1GuZqUO8aOw4Vm/hriuC': '{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"passport":{}}' },
generate: [Function],
_events: { disconnect: [Function], connect: [Function] } }
sessionID: yoOUOxyXZ0SmutA0t5xUr6nI
req.user: undefined
{ cookie:
{ path: '/',
_expires: null,
originalMaxAge: null,
httpOnly: true },
passport: {} }
Update2
I figured out the problem. It was in my user.findByID function:
exports.findById = function(id, callback) {
console.log('Trying to find user with id: ' + id);
db.collection('users').findOne({'_id':id}, function(err, user) {
callback(err, user);
});
};
changed to:
exports.findById = function(id, callback) {
console.log('Trying to find user with id: ' + id);
db.collection('users').findOne({'_id':new BSON.ObjectID(id)}, function(err, user) {
callback(err, user);
});
};
Short for request , the req object is one half of the request and response cycle to examine calls from the client side, make HTTP requests, and handle incoming data whether in a string or JSON object. In this article, you will learn about the req object in Express.
Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app. The Passport library provides more than 500 authentication mechanisms, including OAuth, JWT, and simple username and password based authentication.
Passport exposes a login() function on req (also aliased as logIn() ) that can be used to establish a login session. req. login(user, function(err) { if (err) { return next(err); } return res.
As you said in you update it was the user._id variable not being in a valid format. To avoid this and having to take care of this format later in other requests and methods I will advice you to generate a new user id at signup.
You can use this module:
node-uuid
var uuid = require('node-uuid');
function findOrCreate(profile, callback) {
// save new profile
profile.uid = uuid.v1().replace(/\-/g, '');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With