Skip to content

Commit

Permalink
Refactor profile parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Mar 9, 2014
1 parent 52fa401 commit 7c8b9a3
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions lib/strategy.js
Expand Up @@ -3,6 +3,7 @@
*/
var util = require('util')
, OAuth2Strategy = require('passport-oauth2')
, Profile = require('./profile')
, InternalOAuthError = require('passport-oauth2').InternalOAuthError;


Expand Down Expand Up @@ -88,25 +89,24 @@ util.inherits(Strategy, OAuth2Strategy);
*/
Strategy.prototype.userProfile = function(accessToken, done) {
this._oauth2.get(this._userProfileURL, accessToken, function (err, body, res) {
if (err) { return done(new InternalOAuthError('Failed to fetch user profile', err)); }

var json;

if (err) {
return done(new InternalOAuthError('Failed to fetch user profile', err));
}

try {
var json = JSON.parse(body);

var profile = { provider: 'github' };
profile.id = String(json.id);
profile.displayName = json.name;
profile.username = json.login;
profile.profileUrl = json.html_url;
profile.emails = [{ value: json.email }];

profile._raw = body;
profile._json = json;

done(null, profile);
} catch(e) {
done(e);
json = JSON.parse(body);
} catch (ex) {
return done(new Error('Failed to parse user profile'));
}

var profile = Profile.parse(json);
profile.provider = 'github';
profile._raw = body;
profile._json = json;

done(null, profile);
});
}

Expand Down

0 comments on commit 7c8b9a3

Please sign in to comment.