Search This Blog

Javascript function - Facebook oAuth function for login to application

Hello friends,After such a long period time.We back again :)

So,I have code for login with facebook oAuth using Jquery.That is based on token based call.
you can use this code in simple html ,.net and php applications.

//here we write code for fb oAuth

  <script type="text/javascript">
          window.fbAsyncInit = function () {
                  appId: '****************',//you can get this appId from Facebook developer
                  status: true,
                  cookie: true,
                  xfbml: true,
                  oauth: true,
          (function (d) {
              var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; }
              js = d.createElement('script'); = id; js.async = true;
              js.src = "//";

//this is the login function to login based on token
 function fb_login() {

              FB.login(function (response) {
                  if (response.authResponse) {

                      console.log('Welcome!  Fetching your information.... ');
                      access_token = response.authResponse.accessToken; //get access token
                      user_id = response.authResponse.userID; //get FB UID

                      FB.api('/me', function (response) {
                          if ( {

                              //this details you can store for authentication of users//

                              var userName =;
                              var r = splitUsername(userName);
                              var SNfirstName = r.firstName;
                              var SNlastName = r.lastName;
                              var SNuserId =;
                              var SNuserEmail =;
                              var args = {
                                  "SNfirstName": SNfirstName, "SNuserId": SNuserId, "SNlastName": SNlastName, "SNuserEmail": SNuserEmail
                                  type: "POST",
                                  url: "edRegisterPage.aspx/SetSession",
                                  data: JSON.stringify(args),
                                  contentType: "application/json; charset=utf-8",
                                  dataType: "json",
                                  success: function () {
                                      var url = "/edRegisterPage.aspx";
                                      window.location.href = url;
                                  error: function () {
                  } else {

                 //here you can send alert for login failed !!
                      alert('Login Failed!');
              }, { scope: 'publish_stream,email' });


//html code for button

      <a href="javascript:void(0);" class="btn btn-primary btn-circle"><i class="fa fa-facebook fa-2x" onclick="fb_login();"></i></a>

If any query or any suggestion please write comment.

Bhupinder Singh

I am an Computer Science and Engineering Graduate working in the field of software developing for 5 years. I like programming, web developing. Freelance Programming and writing are my hobbies.

Post A Comment
  • Blogger Comment using Blogger
  • Facebook Comment using Facebook
  • Disqus Comment using Disqus

No comments :

Great! You've decided to leave a comment! Please bear in mind that comments are often moderated and that rel="nofollow" is in use and spammy comments will be deleted. Let's have a meaningful conversation instead. Thanks for stopping by!