Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FB.Event.subscribe not working

I am working on a website and wanted to give my users an option to log-in using facebook.
I am successful at logging in but events are not working for me.

Whenever i logged in, the function "login" was not called.....I have tried assigning this login function to other button for checking it manually, the alert worked but it didnt get any response.

Here my Code:

<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
   FB.init({appId: MY_APP_ID, status: true, cookie: true, xfbml: true});

   FB.Event.subscribe('auth.login', function(response) {
      login();
     });
};

 function login(){
    FB.api('/me', function(response) {
   alert('You have successfully logged in, '+response.name+"!");
  }); 
 }

 (function() {
   var e = document.createElement('script');
   e.type = 'text/javascript';
   e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
   document.getElementById('fb-root').appendChild(e);
 }());     </script>

<fb:login-button autologoutlink='true'   
  perms='email,user_birthday,status_update,publish_stream'></fb:login-button>


This is the facebook application setting:
enter image description here

like image 265
TaLha Khan Avatar asked Jun 10 '26 01:06

TaLha Khan


1 Answers

Have you tried loading the all.js script in the <head> instead of #fb-root? Use the following:

  (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/es_LA/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));

instead of:

 (function() {
   var e = document.createElement('script');
   e.type = 'text/javascript';
   e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
   document.getElementById('fb-root').appendChild(e);
 }());

[EDIT: updated sample]

The SDK inserts elements into fb-root which expect to be positioned relative to the body or relative to an element close to the top of the page.

Which might replacing the SDK source as well.
resource: Facebook Javascript SDK.

--
can you try to subscribe to other event and see if its work

FB.Event.subscribe('auth.statusChange', function(response) {
  // do something with response
});

From Facebook documentation (try subscribing this instead)

auth.authResponseChange
This event is fired for any auth related change as they all affect the session: login, logout, session refresh. Sessions are refreshed over time as long as the user is active with your app.

auth.statusChange
Typically you will want to use the auth.authResponseChange event. But in rare cases, you want to distinguish between these three states:
1. Connected
2. Logged into Facebook but not connected with your application
3. Not logged into Facebook at all.

like image 74
Ikmal Ezzani Avatar answered Jun 11 '26 17:06

Ikmal Ezzani