Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

facebook response body parsed as a json object #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions lib/fboauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ exports = module.exports = FBOAuth2;
/**
* Facebook OAuth2 object, make various requests
* the the graphAPI
* @param {string} appId the facebook app id
* @param {string} appSecret the facebook app secret
* @param {string} redirectURL the url facebook should use as a callback

* @param {object} config the object contains the waterlock-facebook-auth method config in config/waterlock.js
profileQuery {string} the query to use to get data from the graph API
appId {string} the facebook app id
appSecret {string} the facebook app secret
redirectURL {string} the url facebook should use as a callback
*/
function FBOAuth2(appId, appSecret, redirectURL){
this._appId = appId;
this._appSecret = appSecret;
this._redirectURL = redirectURL;
function FBOAuth2(method){
this._appId = method.appId;
this._appSecret = method.appSecret;
this._redirectURL = method.fbOAuth2Url;
this._authURL = 'https://www.facebook.com/dialog/oauth';
this._graphAPIURL = 'https://graph.facebook.com';
this._accessTokenURI = '/oauth/access_token';
this._profileURI = '/me';
this._profileURI = method.profileQuery;
this._scopes = method.scopes;
}

/**
Expand All @@ -43,7 +47,7 @@ FBOAuth2.prototype.getLoginDialogURI = function(){
client_id: this._appId,
redirect_uri: redirect,
response_type: 'code',
scope: 'public_profile,email'
scope: this._scopes
};

return this._authURL + '?' + this.toQuery(params);
Expand Down Expand Up @@ -82,7 +86,7 @@ FBOAuth2.prototype._confirmIdentity = function(err, response, body){
this._confirmCallback(err);
}

this.accessToken = this.toObject(body);
this.accessToken = JSON.parse(body);
this._confirmCallback(null, this.accessToken);
};

Expand Down Expand Up @@ -132,7 +136,10 @@ FBOAuth2.prototype.get = function(uri, cb){
if(typeof this.accessToken === 'undefined'){
cb('Access token does not exist!');
}

var url = this._graphAPIURL + uri + '?' + this.toQuery(this.accessToken);
request(url, cb);
request.get({
url: this._graphAPIURL + uri,
auth: {
bearer: this.accessToken.access_token
}
},cb);
};
10 changes: 4 additions & 6 deletions lib/waterlock-facebook-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@ if (typeof config.authMethod[0] === 'object') {
method = config.authMethod;
}

var fbOAuth2Url;

if (method.redirectUri) {
fbOAuth2Url = method.redirectUri;
method.fbOAuth2Url = method.redirectUri;
} else {
if (config.pluralizeEndpoints) {
fbOAuth2Url = config.baseUrl + '/auths/facebook_oauth2';
method.fbOAuth2Url = config.baseUrl + '/auths/facebook_oauth2';
} else {
fbOAuth2Url = config.baseUrl + '/auth/facebook_oauth2';
method.fbOAuth2Url = config.baseUrl + '/auth/facebook_oauth2';
}
}

exports.config = config;
exports.authConfig = method;
exports.fb = new FBOAuth2(method.appId, method.appSecret, fbOAuth2Url);
exports.fb = new FBOAuth2(method);

exports.actions = require('./controllers');

Expand Down