Monday, 14 April 2014

Facebook javascript sdk for login popup

Facebook provides a large set of client-side functionalities that includes Like buttons, Social plugins etc. But what  I used is javascript SDK for purposes of authenticate user and  fetch information of the user.
 In ruby on rails it is not difficult to login with facebook using gem omniauth, but It redirects to next page or next tab. So, to show popup for facebook authentication, i am using javascript sdk provided by facebook.

Steps to Implement:-
  1. Write the following code in your app/views/layouts/application.html.erb where the the body tag starts.
     <div id="fb-root"></div>     // fb-root is facebook standard id. Please do not change it

       window.fbAsyncInit = function () {
           FB.init({ appId: 'appid', cookie: true, xfbml: true, oauth: true });

           if (typeof facebookInit == 'function') {

           var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
           js = d.createElement('script'); = id; js.async = true;
           js.src = "//";

    • This given script will automatically include facebook javascript and css.
    • The appid is provided by the facebook.
    • Here if condition checks whether the javascript is loaded or not.
    • fb-root is id provided by facebook. Please do not change it.

  1. Now after that, We have to show the login popup on clicking any button. Write this code in html file under the body tag.
<button id="facebook-login" class="button submit-bttn">Facebook Popup</button>
<div id=”status-login”></div>
<div class=”error-vote-poup”></div>
// Using jquery for onclick event
$(‘#facebook-login’).on(‘click’, function(){
function loginPopup() {
FB.login(function(response) {
   if (response.authResponse)
   } else{
console.log('User cancelled login or did not fully authorize.');
},{scope: 'email,user_birthday'});       // birthday is important
function getUserInfo(){
FB.api('/me', function(response) {
var str="<b>Name</b> : ""<br>";
str +="<b>Email:</b> ""<br>";

// this is other info you can get from facebook.
// email:, name:, bio:,  
// location:, uid:, gender:
// response.gender, birthdate: response.birthday
// this ajax is optional (only if you want to hit server with facebook  
// info.)
type: "PUT",
url: "sessions/update_facebook_info",
data: { email:, name:, bio:, location:, uid:, gender: response.gender, birthdate: response.birthday },
statusCode: {
404: function() {
alert( "page not found" );
200: function() {
201: function( msg ) {
400: function( msg ) {

  • In loginPopup function we have to define the scope for the facebook. Reason behind that facebook does not provide us email and birth date untill we define in the scope.
  • In getUserInfo function we will get the response from the facebook in which we we have all the information about the authenticated user.
  • In this function i also define the ajax call with status codes and if we want to hit the server with this information.
  • Now we have all the information regarding the user and we can do the needful.