Question

My app has a Facebook login button (<fb:login-button>) which logs the user into Facebook and authenticates their use of my app. Here is my code (login() and logout() prepare the page layout for being logged in and logged out, respectively). See this answer for the reasoning behind this code.

window.fbAsyncInit = function() {
   FB.init({
        appId: myAppID,
        channelUrl: window.location.protocol + '//' 
            + window.location.host + '/channel.html',
        status: false,
        cookie: true,
        xfbml: false,
        oauth: true
    });

    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            login(response);
        } else {
            FB.Event.subscribe('auth.login', function(response) {
                login(response);
            });
            logout();
        }
    });
}

(function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) return;
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
}(document));

The problem: new users get the Oauth dialog and can log into Facebook, but auth.login doesn't fire for them. Neither do auth.authResponseChange and auth.statusChange (all described here). The next time the user presses the button, or if they were already logged into Facebook, the dialog pops up and immediately disappears. (Strangely, the callbacks do occur as expected with my own Facebook account, perhaps because I've authenticated the app previously.)

As a work-around, I've tried triggering FB.login() as a manual onclick event for the login-button (pardon the use of inline Javascript):

<fb:login-button onclick="FB.login(function(r){if(r.authResponse)login(r)})">

but this doesn't trigger the login callback either (though it does log the user in).

What is the correct way to login to Facebook with a login button?

Était-ce utile?

La solution

Turns out any of these ways to login are fine, I just forgot to disable "Sandbox mode" in my app's settings on developers.facebook.com:

"If enabled, only app developers will be able to use app."

Whoops.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top