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.
Best,
Sami