facebook authentication



https://developers.facebook.com/apps/


declare var FB: any;

ngOninit()
=====================
 (window as any).fbAsyncInit = function() {
    FB.init({
      appId      : '1387547688082396',
      cookie     : true,
      xfbml      : true,
      version    : 'v3.1'
    });
    FB.AppEvents.logPageView();
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "https://connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
 
appcomponent.html:
======================
<button (click)="submitLogin()">Facebook Authentication</button>


appcomponent.ts
======================
submitLogin(){
    console.log("submit login to facebook");
    // FB.login();
    FB.login((response)=>
        {
          console.log('submitLogin',response);
          if (response.authResponse)
          {
            this.toastr.successToastr('login successful', 'Success!');
          }
           else
           {
           console.log('User login failed');
         }
      });

  }
 

Comments

  1. We did same as it is... as you told..but in one system we are getting error "FB is not defined" but in other systems/projects it is working. Not getting how to solve it. Please let me know. Thanks in advance.

    ReplyDelete
    Replies
    1. make sure to declare FB as a variable at the top.

      Then use this.FB instead of FB.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. erro in:"js.src = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);" can you help me

    ReplyDelete
    Replies
    1. do a cast. On js = d.createElement(s) as HTMLScriptElement;

      Delete
  4. fjs.parentNode :Object is possibly 'null'

    ReplyDelete

Post a Comment