Skip to content

Commit

Permalink
Merge pull request #1 from rafaelharus/master
Browse files Browse the repository at this point in the history
 Adding locale in options constructor Strategy
  • Loading branch information
Lansoweb authored Mar 25, 2020
2 parents 9d246c9 + 691dd47 commit 921dcac
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 79 deletions.
20 changes: 12 additions & 8 deletions lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var OAuth2Strategy = require('passport-oauth2')
function Strategy(options, verify) {
options = options || {};
var version = options.graphAPIVersion || 'v3.2';

options.authorizationURL = options.authorizationURL || 'https://www.facebook.com/' + version + '/dialog/oauth';
options.tokenURL = options.tokenURL || 'https://graph.facebook.com/' + version + '/oauth/access_token';
options.scopeSeparator = options.scopeSeparator || ',';
Expand All @@ -59,6 +59,7 @@ function Strategy(options, verify) {
this._profileFields = options.profileFields || null;
this._enableProof = options.enableProof;
this._clientSecret = options.clientSecret;
this._locale = options.locale || null;
}

// Inherit from `OAuth2Strategy`.
Expand Down Expand Up @@ -102,7 +103,7 @@ Strategy.prototype.authorizationParams = function (options) {
if (options.display) {
params.display = options.display;
}

// https://developers.facebook.com/docs/facebook-login/reauthentication/
if (options.authType) {
params.auth_type = options.authType;
Expand Down Expand Up @@ -143,32 +144,35 @@ Strategy.prototype.userProfile = function(accessToken, done) {
// secret as the key.
//
// For further details, refer to:
// https://developers.facebook.com/docs/reference/api/securing-graph-api/
// https://developers.facebook.com/docs/reference/api/securing-graph-api/
var proof = crypto.createHmac('sha256', this._clientSecret).update(accessToken).digest('hex');
url.search = (url.search ? url.search + '&' : '') + 'appsecret_proof=' + proof;
}
if (this._profileFields) {
var fields = this._convertProfileFields(this._profileFields);
if (fields !== '') { url.search = (url.search ? url.search + '&' : '') + 'fields=' + fields; }
}
if (this._locale) {
url.search = (url.search ? url.search + '&' : '') + 'locale=' + this._locale;
}
url = uri.format(url);

this._oauth2.get(url, accessToken, function (err, body, res) {
var json;

if (err) {
if (err.data) {
try {
json = JSON.parse(err.data);
} catch (_) {}
}

if (json && json.error && typeof json.error == 'object') {
return done(new FacebookGraphAPIError(json.error.message, json.error.type, json.error.code, json.error.error_subcode, json.error.fbtrace_id));
}
return done(new InternalOAuthError('Failed to fetch user profile', err));
}

try {
json = JSON.parse(body);
} catch (ex) {
Expand Down Expand Up @@ -219,9 +223,9 @@ Strategy.prototype._convertProfileFields = function(profileFields) {
'emails': 'email',
'photos': 'picture'
};

var fields = [];

profileFields.forEach(function(f) {
// return raw Facebook profile field to support the many fields that don't
// map cleanly to Portable Contacts
Expand Down
Loading

0 comments on commit 921dcac

Please sign in to comment.