I’m trying to integrate passport into my nodejs server using connect, but can’t seem to do it properly. All the guides/examples use expressJS, so I tried my best to reformat the code to work with my code, but I can’t seem to get it to work. The related parts are written below. Does anybody have any advice on what might be the problem? passport.authenticate() never seems to be called (at least the console.log message within the facebook authentication callback never prints). I’m currently not saving anything to a database, so the issue should hopefully be something really simple that I’m just missing.
The only thing that comes to mind is the potential callback I have for facebook, which is a localhost url (since i’m still developing this locally). I was able to authenticate with facebook just fine using everyauth (from a purely local instance), but switched to passportJS since this I was having different issues there that passportJS seemed to address.
passport = require('passport');
fpass = require('passport-facebook').Strategy;
passport.serializeUser(function(user,done){
done(null, user);
});
passport.deserializeUser(function(obj,done){
done(null,obj);
});
passport.use(new fpass({
clientID:'facebook app id',
clientSecret:'facebook app secret',
callbackURL:'http://localhost:3000/auth/facebook/callback'
},
function(accessToken, refreshToken, fbUserData, done){
console.log('got here');
return done(null,fbUserData);
}
));
function checkLoggedIn(req, res, next){
console.log("req.user: " + req.user);
if(req.user)
next();
else{
console.log('\nNot LOGGED IN\n');
if(req.socket.remoteAddress || req.socket.socket.remoteAddress == '127.0.0.1'){
var folder,contentType;
console.log('req url = '+req.url);
if(req.url == '/'){
folder = __dirname + '/landingPage.html';
contentType = 'text/html';
}
else if(req.url == '/auth/facebook'){
passport.authenticate('facebook');
return;
}
else if(req.url == '/auth/facebook/callback'){
passport.authenticate('facebook', {failureRedirect: '/failbook', successRedirect:'/'});
return;
}
if(folder){
console.log('got to folder part\n\n');
fs.readFile(folder, function(error, content){
if(error){
res.writeHead(500);
res.end();
}
else{
res.writeHead(200, {'Content-Type': contentType});
res.end(content);
}
});
}
else{ res.writeHead(500); res.end();}
}
else {res.writeHead(500); res.end();}
}
}
connect.createServer(
connect.cookieParser(),
connect.bodyParser(),
connect.session({secret:'wakajakamadaka'}),
passport.initialize(),
passport.session(),
checkLoggedIn).listen(8888);
console.log('Server has started.');
}
Does anybody have any advice or see a glitch in what I’m doing? My other two alternatives are switching back to everyauth and figuring out what’s going on there, or switching to ExpressJS, but I’d rather not go with either of those options.
passport.authenticatereturns a function which is typically used in the middleware chain, which gets invoked withreqandresby Express.It will work standalone inside other middleware or a route handler, such as your
checkLoggedInfunction, but you have to explicitly invoke the function returned withreq,res, andnextto let it process the request.Looks good otherwise. Let me know if that gets you up and running.
UPDATE
Jared has helped me a little bit outside of stackoverflow, and we have figured out the problem fully. I’m updating this answer to include the new information we found.
1) One issue is clearly that the redirect() middleware exists in Express but not in Connect (at least not the current version). When using connect, you’ll need to change line 97 of authenticate.js in the PassportJS module from
return res.redirect(options.successRedirect);to:note – the
options.successRedirectabove is the same as thesuccessRedirectin the callback function code. This would be the linepassport.authenticate('facebook', {failureRedirect: '/failbook', successRedirect:'/'});in the code in my question.2) The other is that I’m using the cluster module in my app (not something that shows in my code snippet above). This means that session data isn’t being read properly by the nodeJS app, which resulted in my problems. This problem will affect any library that relies on sessions, including all other authentication modules that use sessions (e.g. everyauth, connect-auth, etc.) I will quote Jared for a solution:
Hope this helps whoever makes it here!