Permalink
Browse files

Simplifications to profile parsing.

  • Loading branch information...
jaredhanson committed Feb 10, 2013
1 parent 4428874 commit 4b3af575e9240224af69647b7c3385c81005a90a
Showing with 19 additions and 25 deletions.
  1. +19 −25 lib/passport-facebook/strategy.js
@@ -46,10 +46,9 @@ function Strategy(options, verify) {
options.tokenURL = options.tokenURL || 'https://graph.facebook.com/oauth/access_token';
options.scopeSeparator = options.scopeSeparator || ',';
- this.profileFields = options.profileFields || [];
-
OAuth2Strategy.call(this, options, verify);
this.name = 'facebook';
+ this._profileFields = options.profileFields || null;
}
/**
@@ -100,11 +99,11 @@ Strategy.prototype.authorizationParams = function (options) {
* @api protected
*/
Strategy.prototype.userProfile = function(accessToken, done) {
- var self = this,
- fields = this._convertProfileFields(this.profileFields);
-
- var url = 'https://graph.facebook.com/me'
- + (fields !== '' ? '?fields=' + fields : '');
+ var url = 'https://graph.facebook.com/me';
+ if (this._profileFields) {
+ var fields = this._convertProfileFields(this._profileFields);
+ url += (fields !== '' ? '?fields=' + fields : '');
+ }
this._oauth2.getProtectedResource(url, accessToken, function (err, body, res) {
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }
@@ -116,21 +115,21 @@ Strategy.prototype.userProfile = function(accessToken, done) {
profile.id = json.id;
profile.username = json.username;
profile.displayName = json.name;
-
- if (self._includeProfileField('name')) {
- profile.name = { familyName: json.last_name,
- givenName: json.first_name,
- middleName: json.middle_name };
- }
+ profile.name = { familyName: json.last_name,
+ givenName: json.first_name,
+ middleName: json.middle_name };
profile.gender = json.gender;
profile.profileUrl = json.link;
-
- if (self._includeProfileField('emails')) {
- profile.emails = [{ value: json.email }];
- }
- if (self._includeProfileField('photos')) {
- profile.photos = [{ value: json.picture }];
+ profile.emails = [{ value: json.email }];
+
+ if (json.picture) {
+ if (typeof json.picture == 'object' && json.picture.data) {
+ // October 2012 Breaking Changes
+ profile.photos = [{ value: json.picture.data.url }];
+ } else {
+ profile.photos = [{ value: json.picture }];
+ }
}
profile._raw = body;
@@ -143,11 +142,6 @@ Strategy.prototype.userProfile = function(accessToken, done) {
});
}
-Strategy.prototype._includeProfileField = function(field) {
- return this.profileFields.length === 0
- || this.profileFields.indexOf(field) !== -1;
-}
-
Strategy.prototype._convertProfileFields = function(profileFields) {
var fields = [];
var fieldsMap = {
@@ -160,7 +154,7 @@ Strategy.prototype._convertProfileFields = function(profileFields) {
'emails': 'email',
'photos': 'picture'
};
- profileFields.forEach(function (f) {
+ profileFields.forEach(function(f) {
if (typeof fieldsMap[f] === 'undefined') return;
if (Array.isArray(fieldsMap[f])) {

0 comments on commit 4b3af57

Please sign in to comment.