Facebook OAuth “Unsupported” in Chrome on iOS

后端 未结 7 1580
我寻月下人不归
我寻月下人不归 2020-12-01 04:38

The Facebook OAuth popup is throwing an error in Chrome on iOS only. Both developers.facebook.com and google have turned up nothing about this. Ideas?

相关标签:
7条回答
  • 2020-12-01 04:51

    Here is a complete workaround for your FB JS Auth on Chrome iOS issue http://seanshadmand.com/2015/03/06/facebook-js-login-on-chrome-ios-workaround/

    JS functions to check auth, open FB auth page manually and refresh auth tokens on original page once complete:

    function openFBLoginDialogManually(){
      // Open your auth window containing FB auth page 
      // with forward URL to your Opened Window handler page (below)
    
      var redirect_uri = "&redirect_uri=" + ABSOLUTE_URI + "fbjscomplete";
      var scope = "&scope=public_profile,email,user_friends";
      var url = "https://www.facebook.com/dialog/oauth?client_id=" + FB_ID + redirect_uri + scope;
    
      // notice the lack of other param in window.open
      // for some reason the opener is set to null
      // and the opened window can NOT reference it
      // if params are passed. #Chrome iOS Bug
      window.open(url);
    
    }
    
    function fbCompleteLogin(){
    
      FB.getLoginStatus(function(response) {
        // Calling this with the extra setting "true" forces
        // a non-cached request and updates the FB cache.
        // Since the auth login elsewhere validated the user
        // this update will now asyncronously mark the user as authed
      }, true);
    
    }
    
    function requireLogin(callback){
    
        FB.getLoginStatus(function(response) {
            if (response.status != "connected"){
                showLogin();
            }else{
                checkAuth(response.authResponse.accessToken, response.authResponse.userID, function(success){
                  // Check FB tokens against your API to make sure user is valid
                });
            }
        });
    
    }
    

    And the Opener Handler that FB auth forwards to and calls a refresh to the main page. Note the window.open in Chrome iOS has bugs too so call it correctly as noted above:

    <html>
    <head>
    <script type="text/javascript">
    function handleAuth(){
        // once the window is open 
        window.opener.fbCompleteLogin();
        window.close();    
    }
    </script>
    <body onload="handleAuth();">
        <p>. . . </p>
    </body>
    </head>
    </html>
    
    0 讨论(0)
  • 2020-12-01 04:53

    My 2 cents on this as noone of the answers were clear to me. Im firing the login js dialog on a button click, so now when it's chrome ios I check first if the user it's logged into facebook and if not I send them to the login window. The problem with this is that chome ios users needs to click connect button twice if they are not logged into facebook. If they are logged into facebook one click is enough.

     $( 'body' ).on( 'click', '.js-fbl', function( e ) {
            e.preventDefault();
    
            if( navigator.userAgent.match('CriOS') ) {
                // alert users they will need to click again, don't use alert or popup will be blocked
                $('<p class="fbl_error">MESSAGE HERE</p>').insertAfter( $(this));
                FB.getLoginStatus( handleResponse );
            } else {
                // regular users simple login
                try {
                    FB.login( handleResponse , {
                        scope: fbl.scopes,
                        return_scopes: true,
                        auth_type: 'rerequest'
                    });
                } catch (err) {
                    $this.removeClass('fbl-loading');
    
                }
            }
        });
    

    That bit of code make it works for chrome ios users. On handle response I simple take care of fb response and send it to my website backend for login/register users.

    var handleResponse = function( response ) {
        var $form_obj       = window.fbl_button.parents('.flp_wrapper').find('form') || false,
            $redirect_to    = $form_obj.find('input[name="redirect_to"]').val() || window.fbl_button.data('redirect');
        /**
         * If we get a successful authorization response we handle it
         */
        if (response.status == 'connected') {
    
            var fb_response = response;
    
            /**
             * Make an Ajax request to the "facebook_login" function
             * passing the params: username, fb_id and email.
             *
             * @note Not all users have user names, but all have email
             * @note Must set global to false to prevent gloabl ajax methods
             */
            $.ajax({...});
    
        } else {
             //if in first click user is not logged into their facebook we then show the login window
             window.fbl_button.removeClass('fbl-loading');
            if( navigator.userAgent.match('CriOS') )
                window.open('https://www.facebook.com/dialog/oauth?client_id=' + fbl.appId + '&redirect_uri=' + document.location.href + '&scope=email,public_profile', '', null);
        }
    };
    

    Hope it helps!

    0 讨论(0)
  • 2020-12-01 04:57

    Not a real answer but based on this thread worth noting that is started working for our app, on Chrome when on the iPhone we did General>Reset>Reset Location & Privacy

    0 讨论(0)
  • 2020-12-01 05:00

    I got a solution for ios facebook website login in google chrome . Actually the issue was with google chrome in ios when we click on facebook login button it give internally null to the window.open in ios chrome .
    There are two solution either to check it is chrome in ios(chrios) and then generate custom login screen ( still not chance that it will we correct ).
    Second what i have used. Is to use facebook login from backhand create a api hit will populate facebook screen and then when login is done it will redirect to your server from where you will redirect to your website page with facebook data.
    one other benefit of it is that you create 2 website for same website owner you can not set two website url in facebook developer account .In this way you can create many website facebook login with same facebook appid .

    0 讨论(0)
  • 2020-12-01 05:12

    This is a very common issue which all developers have faced while implementing the FB login feature. I have tried most of the Internet solutions but none of them worked. Either window.opener do not work in Chrome iOS or sometime FB object is not loaded while using /dialog/oauth.

    Finally I solved this by myself after trying all the hacks!

    function loginWithFacebook()
    {
      if( navigator.userAgent.match('CriOS') )
      {
        var redirect_uri = document.location.href;
        if(redirect_uri.indexOf('?') !== -1)
        {
          redirect_uri += '&back_from_fb=1';
        }
        else
        {
          redirect_uri += '?back_from_fb=1';
        }
        var url = 'https://www.facebook.com/dialog/oauth?client_id=[app-id]&redirect_uri='+redirect_uri+'&scope=email,public_profile';
        var win = window.open(url, '_self');
      }
      else
      {
        FB.login(function(response)
          {
            checkLoginState();
          },
          {
            scope:'public_profile,email,user_friends,user_photos'
          });
      }
    }
    

    Notice that above I'm passing an extra param to the redirect url so that once the new window opens with above redirect uri I could read the values and can say yes this call is from Chrome iOS window. Also make sure this code runs on page load.

    if (document.URL.indexOf('back_from_fb=1') != -1 && document.URL.indexOf('code=') != -1)
    {
      pollingInterval = setInterval(function()
        {
          if(typeof FB != "undefined")
          {
            FB.getLoginStatus(function(response) {}, true);
            checkLoginState();
          }
          else
          {
            alert("FB is not responding, Please try again!", function()
              {
                return true;
              });
          }
        }, 1000);
    }
    
    0 讨论(0)
  • 2020-12-01 05:15

    You can use the redirection method as follow for this case (by detecting the user agent being chrome ios):

    https://www.facebook.com/dialog/oauth?client_id={app-id}&redirect_uri={redirect-uri}
    

    See more info here https://developers.facebook.com/docs/facebook-login/login-flow-for-web-no-jssdk/

    Remark: I personnaly use the server OAuth in that case but this should do the trick and is quite simple

    0 讨论(0)
提交回复
热议问题