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 () {
              FB.init({
                  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'); js.id = id; js.async = true;
              js.src = "//connect.facebook.net/en_US/all.js";
              d.getElementsByTagName('head')[0].appendChild(js);
          }(document));



//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 (response.name) {

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

                              var userName = response.name;
                              var r = splitUsername(userName);
                              var SNfirstName = r.firstName;
                              var SNlastName = r.lastName;
                              var SNuserId = response.id;
                              var SNuserEmail = response.email;
                              var args = {
                                  "SNfirstName": SNfirstName, "SNuserId": SNuserId, "SNlastName": SNlastName, "SNuserEmail": SNuserEmail
                              }
                              $.ajax({
                                  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 () {
                                      alert("Fail");
                                  }
                              });
                          }
                      });
                  } else {

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
















   











//html code for button
<ul>





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

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!


Tutorials

[TUTORIALS][bsummary]