From 7c8b9a3328d4bcfb77bae837beca31579cbe859f Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Sun, 9 Mar 2014 10:26:01 -0700 Subject: [PATCH] Refactor profile parsing. --- lib/strategy.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/strategy.js b/lib/strategy.js index c11e698..297b033 100644 --- a/lib/strategy.js +++ b/lib/strategy.js @@ -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); }); }