Search code examples
javascriptfacebookfacebook-javascript-sdkfacebook-loginfacebook-browser

Facebook login is not working in Andriod Facebook in app browser


From a very login time, I am using the Facebook login Javascript SDK on my website, but recently it stopped working in the Android Facebook in-app browser. When I am clicking the login button it reloads the complete page to go to the authorization page, and then came back it got nothing

The sample is given by Facebook also have same issue, have tested using even FB.login function and also with Facebook Login Button ( https://developers.facebook.com/docs/facebook-login/web/login-button ). Got the same result for both of them. It just refreshes the page.

Sample code:

<script>

function LoggedFb(){
      FB.login(function (response) {
                FB.api(`/me?fields=id,email&access_token=${response.authResponse.accessToken}`, function 
                (userResponse) {
                    alert(userResponse.email);
                });
            }, { scope: 'email' })
}

</script>

The code is working on every platform other than the Android Facebook in-App browser.

Thanks.


Solution

  • For me, it was doing a constant loop: signin, refresh and try again with never signing in.

    I was able to fix this by using FB.getLoginStatus instead of just FB.login.

    If the user is already logged in through fb, it will go through. if not, there will be one refresh, but then login will work.

    I would appreciate if someone could fix this so there would be no refresh at all.

    Example code:

    function signIn() {
      FB.getLoginStatus((response: any) => {
        if (response.status !== 'connected') {
          return FB.login((response: any) => {
            handleFbResponse(response);
          }, {
            scope: 'public_profile,email',
            enable_profile_selector: true,
            auth_type: 'rerequest',
            return_scopes: true
          });
        } else {
          handleFbResponse(response);
        }
      });
    }
    
    function handleFbResponse(response) { ... }