Search code examples
javascriptfacebook

What to do after getting 'not_authorized' back from Facebook Connect login


After your get your response from getLoginStatus, is the idea to then call FB.login? Currently when I do that, the code stops.

Here's the code for when they hit the FB button to login, which correctly gives me the response...

function facebookConnectBtnLogin()
{
    FB.getLoginStatus(function(response)
    {
        CL();
        if (response.status === 'connected') {

            logFacebookUserIn(response);            
        } else if (response.status === 'not_authorized') {
        // alert('not_authorized...'); 
         facebookLoginInit();
        } else {
            //alert('not logged into Facebook...');
            facebookLoginInit();
        }
    },true);
}   

And the code that deals with FB.login, which alerts 'here4', but not 'here5'...

function facebookLoginInit()
{
    alert('here4');
    FB.login
    (
        function(response)
        {
            alert('here5');
            CL(); 
            if(response.authResponse)
            {
                alert('here6'); logFacebookUserIn(response);
            } else alert('not connected');
        },
        { scope: "email" }
    );  
}

Do I have the right idea?

Thanks for your time and help.


Solution

  • You should never call FB.login in an asynchronous callback function, ONLY on direct user interaction (mouse click). Browsers block the FB.login popup if you don´t call it on user interaction.

    With FB.getLoginStatus you just check if the user is authorized, if not: present a Login Button where the user can click to login. It´s not a good idea to show the login dialog right when the user enters your App anyway, tell him what the App is about first.