Skip to content

Instantly share code, notes, and snippets.

@jbasdf
Created February 28, 2014 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbasdf/9262863 to your computer and use it in GitHub Desktop.
Save jbasdf/9262863 to your computer and use it in GitHub Desktop.
Facebook OAuth2 client side.
var Facebook = {
init: function(){
if(!GLOBAL_SETTINGS.FBappId){ return; }
if(GLOBAL_SETTINGS.application_name){
Facebook.authorize_message = "Authorize your Facebook account for " + GLOBAL_SETTINGS.application_name;
}
Facebook.load_fb();
$('#facebook_connect').on('click', function(e){
e.preventDefault();
Facebook.login();
});
},
login: function(){
FB.login(function(response){}, { scope: 'publish_stream,email' });
},
finish: function(response){
window.location.href = '/users/auth/facebook/callback?state='+ GLOBAL_SETTINGS.state;
},
message: function(msg){
$('#fb_connect_message').text(msg);
$('#fb_connect_message').show();
},
load_fb: function(){
window.fbAsyncInit = function() {
FB.init({
appId : GLOBAL_SETTINGS.FBappId,
status : false, // don't check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('auth.authResponseChange', function(response) {
if(response.status === 'connected'){
if(response.authResponse){
Facebook.finish(response);
}
} else if (response.status === 'not_authorized'){
Facebook.message(Facebook.authorize_message);
} else {
FB.login();
}
});
};
// Load the FB SDK asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment