Reputation: 2736
I have passport working with Google, and Facebook. I have attempted to add Github to add these credentials so I can do validated Github API calls. So I simply added the same pattern I used to login using either Google or Facebook credentials.
BUT I see InternalOAuthError in the middle of my code after the auth callback has taken place from Github. This happens when the nearly last line: 'return done(null, user.userData);' is called. Attempting to debug interferes with the callbacks. SO I am hoping that someone with greater clarity about passport can explain what I am doing wrong.
What is really strange is that I have already received the user profile from github and stored in in my database with 'user.update(db)', in the same way I do with google. Then the crash happens when I attempt to return by calling done(...).
Do I need to add something to my profile on github? or something else? Or is this because I already have used passport much earlier to login using Google credentials. Note that for Google, or Facebook, I have specified session: false. I have attempted this with both 'passport-github' and 'passport-github2'.
The code for clarity is:
index.js
var express = require('express');
var passport = require('passport');
var auth = require('../auth.service');
var router = express.Router();
router
.get('/:user', passport.authenticate('github', {
failureRedirect: '/signup',
session: false
}))
.get('/callback', passport.authenticate('github', {
failureRedirect: '/signup',
session: true
}), auth.setTokenCookie);
module.exports = router;
and the corresponding passport.js
var passport = require('passport');
var GitHubStrategy = require('passport-github2').Strategy;
var monk_db = rekuire('server/monk_db');
var loggingAndErrors = rekuire('./loggingAndErrors');
var auth = require('../auth.service');
var config = require('../../config/environment');
var jwt = require('jsonwebtoken');
var expressJwt = require('express-jwt');
var validateJwt = expressJwt({ secret: config.secrets.session });
var jwtDecode = require('jwt-decode');
var ObjectID = require("bson-objectid");
exports.setup = function (User, config) {
passport.use(new GitHubStrategy({
clientID: config.github.clientID,
clientSecret: config.github.clientSecret,
callbackURL: config.github.callbackURL,
passReqToCallback: true
},
function(req, accessToken, refreshToken, profile, done) {
//loggingAndErrors.logger.log(accessToken);
var token = req.cookies.token;
var decoded = jwtDecode(token);
var user_id = decoded._id;
var db = monk_db.getDb();
var users = User.getUsers(db);
users.findById(ObjectID(user_id), function(err, user) {
if (err) {
loggingAndErrors.loggerError.log(err);
return done(err);
}
//loggingAndErrors.logger.log(profile);
user.github=profile._json
user = User.newUser(user);
user.update(db).onResolve(function(err, result) {
if (err) {
loggingAndErrors.loggerError.log(err);
return done(err);
}
//loggingAndErrors.logger.log(user);
loggingAndErrors.logger.log("calling done(err, user) for user_id:", user.userData._id);
return done(null, user.userData);
});
});
}
));
};
The crash is:
{ statusCode: 404,
data: '{"message":"Not\
Found","documentation_url":"https://developer.github.com/v3"}' }
GET /auth/github/callback?code=7c0c6dff81cdd9417301 500 945.444 ms - 674
InternalOAuthError: Failed to fetch user profile
at /home/joel/workspace/Tracker2/node_modules/passport-github2/lib/strategy.js:118:21
at passBackControl (/home/joel/workspace/Tracker2/node_modules/passport-github2/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:123:9)
at IncomingMessage.<anonymous> (/home/joel/workspace/Tracker2/node_modules/passport-github2/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:142:7)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickDomainCallback (node.js:381:11)
Upvotes: 2
Views: 1220
Reputation: 821
I just ran into the same problem, so here is what works for me.
Use passport-github2 and request access to the user's email address when authenticating:
passport.authenticate('github', { scope: [ 'user:email' ] }));
There are other permissions that you may want to access at the same listed in the GitHub OAuth documentation.
Upvotes: 4