2

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 !!

Cynthia Sanchez
  • 178
  • 1
  • 9
  • 1
    You could put a breakpoint on `strategy.js` line number 96 and look at the variable `err` to see if it gives you any more information – George Aug 22 '17 at 13:12
  • It doesnt give more details, but all I can see in line 96 is: ```return done(new InternalOAuthError('Failed to fetch user profile', err));``` – Cynthia Sanchez Aug 22 '17 at 13:17
  • And you say this is happening for other strategies (google/facebook), have you tried running the code on another machine and network? – George Aug 22 '17 at 13:24
  • Yes, the same error appears with Facebook as I checked. I don't think trying it in another network or machine would make a difference honestly. – Cynthia Sanchez Aug 22 '17 at 13:50
  • I tried it just in case, but still not working. – Cynthia Sanchez Aug 22 '17 at 19:37

1 Answers1

0

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.

Cynthia Sanchez
  • 178
  • 1
  • 9
  • I think many people might be struggling with this. (I am). What do you mean by 'all in the same file'? – Rambatino Nov 17 '17 at 02:02
  • Rambatino, I was making some mistakes when exporting the module. All the configuration for the passport object was moved into the app.js file and the routes kept in the github.js file that is then exported `module.export = router` to then be imported into app.js – Cynthia Sanchez Feb 20 '18 at 18:26
  • you can see our repo here: https://gitlab.com/SUSE-UIUX/UXUIteamwebsite – Cynthia Sanchez Feb 20 '18 at 18:27