Skip to content

Instantly share code, notes, and snippets.

@gecbla
Created May 10, 2014 22:01
Show Gist options
  • Save gecbla/b31f9af4d592286fe7fc to your computer and use it in GitHub Desktop.
Save gecbla/b31f9af4d592286fe7fc to your computer and use it in GitHub Desktop.
/*global window, define */
var FacebookConnect = {
options: {
appId: null, // {String} Your application ID
cookie: true, // {Boolean} Enables cookies to allow the server to access the session
logging: true, // {Boolean} Disable logging
session: null, // {Object} Use specified session object
status: true, // {Boolean} Fetch fresh status
xfbml: true, // {Boolean} Parse XFBML tags
scope: '', // {String} Comma separated list of permission names
oauth: true // {Boplean} Enables OAuth 2.0 functionality
},
error: {
generic: 'We are unable to process your request at the moment',
signin: 'Sorry, an error interrupted the sign-in process'
},
user: null,
init: function (options) {
options = options || {};
for (var propertyName in this.options) {
if (options.hasOwnProperty(propertyName)) {
this.options[propertyName] = options[propertyName];
}
}
FB.init(this.options);
this.registerEvents();
},
registerEvents: function () {
// run it whenever the status changes
FB.Event.subscribe('auth.statusChange', fbConnect.onStatusUpdate);
},
onStatusUpdate: function (response) {
if (response.authResponse) {
// is connected
console.log('Facebook user is logged in');
} else {
fbConnect.user = null;
}
},
login: function () {
FB.login(function (response) {
if (!response.authResponse) {
console.log('Login error');
return false;
}
console.log("My user ID is: " + response.authResponse.userID);
FB.api('/me', function (response) {
if (response.name) {
console.log('My name is: ' + response.name);
fbConnect.user = response;
} else {
console.log('User not found');
console.log(response);
}
$.fancybox.close();
});
}, {
scope: this.options.scope
});
return false;
},
logout: function () {
FB.logout(function (response) {
// callback
});
},
revokeAuthorization: function () {
FB.api({
method: 'Auth.revokeAuthorization'
}, function (response) {
// callback
});
},
isLoggedIn: function () {
// Returns true if the access token has been received and verified
return this.user && this.user.id;
},
isDialogBoxOpen: function () {
return $('#fancybox-content:empty').length > 0 ? true : false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment