Permalink
Browse files

Refactor profile parsing.

  • Loading branch information...
jaredhanson committed Mar 9, 2014
1 parent 52fa401 commit 7c8b9a3328d4bcfb77bae837beca31579cbe859f
Showing with 17 additions and 17 deletions.
  1. +17 −17 lib/strategy.js
View
@@ -3,6 +3,7 @@
*/
var util = require('util')
, OAuth2Strategy = require('passport-oauth2')
+ , Profile = require('./profile')
, InternalOAuthError = require('passport-oauth2').InternalOAuthError;
@@ -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);
});
}

0 comments on commit 7c8b9a3

Please sign in to comment.