{"id":8084,"date":"2012-09-24T14:02:37","date_gmt":"2012-09-24T08:32:37","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8084"},"modified":"2012-09-24T14:48:33","modified_gmt":"2012-09-24T09:18:33","slug":"implementing-login-with-facebook","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/implementing-login-with-facebook\/","title":{"rendered":"Implementing Login With Facebook"},"content":{"rendered":"<p>Hi,<\/p>\n<p>In my recent grails project i had a requirement to allow an user to login into my site using his Facebook credentials<br \/>\nTo achieve it , here is what is required to be done : <\/p>\n<pre>\r\n\r\n<\/pre>\n<p>1)<strong> Create an application in facebook :<\/strong><br \/>\nCreate an application in facebook from the application dashboard (https:\/\/developers.facebook.com\/apps).<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>2)<strong> Provide the App Domain and Site Url :<\/strong><br \/>\nAfter providing the required appname now it is needed to provide the App Domain and Site Url,<\/p>\n<p>   i) Set App Domain to your domain eg. &#8216;myDomain.com&#8217;.<\/p>\n<p>  ii) Now , go to the section &#8216;Select how your app integrates with Facebook&#8217; and select &#8216;Website with Facebook Login&#8217; and provide<br \/>\n      your site URL eg. &#8216;http:\/\/www.myDomain.com&#8217;.<\/p>\n<p>Note the App ID value , it would be required later.<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>3) <strong> Load the javascript sdk :<\/strong><br \/>\nLoad the javascript sdk now by writing this script in the login page of the application :<\/p>\n<p>[javascript]<\/p>\n<p>      &lt;script&gt;<br \/>\n        window.fbAsyncInit = function() {<br \/>\n          FB.init({<br \/>\n            appId      : &#8216;YOUR_APP_ID&#8217;, \/\/ App ID<br \/>\n            channelUrl : &#8216;\/\/WWW.YOUR_DOMAIN.COM\/channel.html&#8217;, \/\/ Channel File<br \/>\n            status     : true, \/\/ check login status<br \/>\n            cookie     : true, \/\/ enable cookies to allow the server to access the session<br \/>\n            xfbml      : true  \/\/ parse XFBML<br \/>\n          });<br \/>\n          \/\/ Additional initialization code here<br \/>\n        };<br \/>\n        \/\/ Load the SDK Asynchronously<br \/>\n        (function (data) {<br \/>\n                var jse, id = &#8216;facebook-jssdk&#8217;, ref = data.getElementsByTagName(&#8216;script&#8217;)[0];<br \/>\n                if (data.getElementById(id)) {<br \/>\n                    return;<br \/>\n                }<br \/>\n                jse = data.createElement(&#8216;script&#8217;);<br \/>\n                jse.id = id;<br \/>\n                jse.async = true;<br \/>\n                jse.src = &quot;\/\/connect.facebook.net\/en_US\/all.js#xfbml=1&amp;appId=YOUR_APP_ID&quot;;<br \/>\n                ref.parentNode.insertBefore(jse, ref);<br \/>\n            }(document));<br \/>\n      &lt;\/script&gt;<\/p>\n<p>[\/javascript]<\/p>\n<p>4) <strong> Add facebook login button :<\/strong><br \/>\nNow we are Good to go. We would want that when a user is logged in on facebook , he gets logged in on our site as well.<br \/>\n   We can authenticate the user from the information which he has on facebook rather than forcing the user to register for<br \/>\n   our site.For this we need facebook to do the authentication part for us.<br \/>\n   To do authentication we have to add a Facebook login button in our login page as :<\/p>\n<p>[html]<\/p>\n<p>  &lt;div class=&quot;fb-login-button&quot;&gt;Login with Facebook&lt;\/div&gt;<\/p>\n<p>[\/html]<br \/>\nThis provides us the access to user&#8217;s basic information.For getting access to more information as Email we need to ask<br \/>\npermissions from the user by mentioning it in a scope attribute as :<br \/>\n[html]<br \/>\n&lt;div class=&quot;fb-login-button&quot; scope=&quot;email,user_checkins&quot;&gt;<br \/>\n        Login with Facebook<br \/>\n      &lt;\/div&gt;<\/p>\n<p>[\/html]<\/p>\n<p>5)<strong> Get information of the authenticated user :<\/strong><br \/>\nWhen the user is logged in facebook ,we have the information of the authenticated user.To get that information we do:<\/p>\n<p>[javascript]<\/p>\n<p>FB.Event.subscribe(&#8216;auth.login&#8217;, function () {<br \/>\nFB.getLoginStatus(function (response) {<br \/>\n                    if (typeof(response) == &#8216;undefined&#8217;) {<br \/>\n                        return<br \/>\n                    }<br \/>\n                    else {<br \/>\n                        var uid = response.authResponse.userID;<br \/>\n                        var accessToken = response.authResponse.accessToken;<br \/>\n                        getProfileInfoAndLogin(accessToken);<br \/>\n                    }<\/p>\n<p>                });<br \/>\n });<\/p>\n<p> function getProfileInfoAndLogin(token) {<br \/>\n                    var url = &quot;${createLink(controller: &#8216;controllerName&#8217;, action: &#8216;actionName&#8217;)}&quot;;<br \/>\n                    FB.api(&quot;\/me&quot;, &quot;GET&quot;, function (response) {<br \/>\n                 $.ajax({<br \/>\n                            type:&quot;GET&quot;,<br \/>\n                            url:url,<br \/>\n                            dataType:&quot;html&quot;,<br \/>\n                            data:{fbEmail:response.email}<br \/>\n                        }).done(function (data) {<br \/>\n                                    alert(data);<br \/>\n                                    if (data == &quot;true&quot;) {<br \/>\n                                        \/\/ redirect user to his homepage<br \/>\n                                      \/\/ eg.   window.location = &quot;http:\/\/www.example.com\/user\/home&quot;;<br \/>\n                                    }<br \/>\n                                    else {<br \/>\n                                        alert(&quot;failure&quot;)<br \/>\n                                    }<br \/>\n                                });<br \/>\n                    });<br \/>\n                }<br \/>\n[\/javascript]<br \/>\nIn the getProfileInfoAndLogin function, &#8216;function(response)&#8217; is a callback function through which we can access the data facebook sends after passing validation.When we have the data we can manipulate it as we want.<br \/>\nI have send the user&#8217;s email to the server using an Ajax call and checked whether that<br \/>\nEmail exists in database and then logged the user into my site as :<br \/>\n[javascript]<br \/>\n def fbLoginCheck() {<br \/>\n        String userEmail = params.fbEmail<br \/>\n            if (User.countByEmail(userEmail)) {    \/\/ User is already stored in database<br \/>\n             session.userEmail = userEmail<br \/>\n            render true<br \/>\n        }<br \/>\n  }<br \/>\n[\/javascript]<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>6)<strong> Logout user :<\/strong><br \/>\nTo log the user out of our site we add this script to the HOME page of user along with the javascript SDK  :<\/p>\n<p>[javascript]<\/p>\n<p> function fbLogout(){<br \/>\n            FB.logout(function (response) {<br \/>\n\/\/              Do what we want to do when user logs out from facebook like call our logout action.<br \/>\n\/\/              eg.  window.location = &quot;http:\/\/www.example.com\/logout&quot;;<br \/>\n            });<br \/>\n        }<br \/>\n[\/javascript]<\/p>\n<p>The logout button can be added as :<br \/>\n[html]<br \/>\n&lt;span id=&quot;fbLogout&quot; onclick=&quot;fbLogout()&quot;&gt;&lt;a class=&quot;fb_button fb_button_medium&quot;&gt;&lt;span class=&quot;fb_button_text&quot;&gt;Logout&lt;\/span&gt;&lt;\/a&gt;&lt;\/span&gt;<br \/>\n[\/html]<\/p>\n<p>Hope this helps.<br \/>\nCheers. \ud83d\ude42<\/p>\n<p>Vineet Mishra<br \/>\nIntelligrape software<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In my recent grails project i had a requirement to allow an user to login into my site using his Facebook credentials To achieve it , here is what is required to be done : 1) Create an application in facebook : Create an application in facebook from the application dashboard (https:\/\/developers.facebook.com\/apps). 2) Provide [&hellip;]<\/p>\n","protected":false},"author":58,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[7],"tags":[703,1041,700],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8084"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/58"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8084"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8084\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}