I have been struggling a lot trying to have passport working. I am using passport-github2, but while trying to troubleshoot the issue I tried passport-facebook and twitter as well, and none of them seem to work now.
I had a simple application which was working with facebook/twitter sign in, and now they don't work anymore, so it seems this has to do with a newer version of Passport.
Here is what I have in my github.js file, which is accessed with url api/auth/github
var express = require('express'),
router = express.Router(),
config = require('config'),
githubAuth = config.get('github'),
githubStrategy = require('passport-github2').Strategy,
flash = require('connect-flash'),
GITHUB_CLIENT_ID = githubAuth.githubClientID,
GITHUB_CLIENT_SECRET = githubAuth.githubClientSecret,
app = express();
//passport vars
var passport = require('passport'),
session = require('express-session'),
methodOverride = require('method-override'),
bodyParser = require("body-parser"),
cookieParser = require("cookie-parser");
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
passport.use(new githubStrategy({
clientID: GITHUB_CLIENT_ID,
clientSecret: GITHUB_CLIENT_SECRET,
callbackURL: 'http://localhost:3000/api/auth/github/callback'
},
function (token, tokenSecret, profile, done) {
// asynchronous verification, for effect...
return done(null, profile);
}
));
app.use(cookieParser());
app.use(bodyParser());
app.use(methodOverride());
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash())
router.get('/', passport.authenticate('github', {
scope: ['user']
}));
router.get('/callback',
passport.authenticate('github', {
failureRedirect: '/',
successRedirect: '/'
}),
function (req, res) {
res.send('Hello World!!');
}
);
module.exports = router;
When i go to http://localhost:3000/api/auth/github
github lets me authenticate and I click on the green button that will take me back to the callback url, and there is where everything explodes and I see this message:
Error
at /Users/cynt005/Documents/Code/UXUIteamwebsite/node_modules/passport-github2/lib/strategy.js:96:19
at passBackControl (/Users/cynt005/Documents/Code/UXUIteamwebsite/node_modules/oauth/lib/oauth2.js:132:9)
at IncomingMessage.<anonymous> (/Users/cynt005/Documents/Code/UXUIteamwebsite/node_modules/oauth/lib/oauth2.js:157:7)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1059:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
Console:
GET http://localhost:3000/api/auth/github/callback?code=60a130c5c54a6fc30266 500 (Internal Server Error)
I have googled all across the web, and I dont find a solution that works for me. There is a thread in github but no solution has yet been provided officially:
https://github.com/jaredhanson/passport-facebook/issues/100
Any help will be appreciated !!
Solved. The issue was that I had to keep everything in the same file as app.js
I still would like to know if there is a better/tidy way to separate this file without breaking the code.