Google+ Sign-In with JavaScript callback issue

前端 未结 6 2345
轮回少年
轮回少年 2021-02-08 13:01

I am working on a function which allows users to sign in on my website with their Google account.

My code is based on the Google documentation (others signIn() options a

相关标签:
6条回答
  • 2021-02-08 13:23

    finally i solved with a workaround; i don't know if this is the correct way to approach or i am just cheating but i do this way:

    first of all some script in the page (i am using bootstrap + jquery)

    function render() {
        //I am not using it but kept anyway
    
     }
    var i;
    // Function called from a onClick on a link or button (the 'sign in with g+' button)
    function gp_login() {
        i=0;
        $('#alertbox').remove();
        var additionalParams = {
         'callback': signinCallback,
         /*'approvalprompt': 'force' finally removed*/
        };
        $('#gp-login').button('loading');
        gapi.auth.signIn(additionalParams); 
    }
    
    function signinCallback(authResult) { //my callback function
            var email='';
            var given_name='';
            if (authResult['status']['signed_in']) { //get some user info
                gapi.client.load('oauth2', 'v2', function() {
                    gapi.client.oauth2.userinfo.get().execute(function(resp){
                        email = resp.email; //get user email
                        given_name = resp.given_name; //get user email
                        family_name=resp.family_name;
                        id=resp.id;
                        if (i<2) { //execute the doLogin just one time (my cheat)
                            doLogin(email,given_name,family_name,id); //dologin does my logic with an ajax call to signup/register user to my site
                        }
                        i=2;
                    });
                });
            } else {
            // Update the app to reflect a signed out user
          }
    
    }
    

    this approch have the doLogin part called just one time, but the callback is called twice (gapi.client.oauth2.userinfo.get() this function is called twice); with a bit more tweaking with the if / var check i think is possible to call everything once. This way if the user already granted the auth, it will be automatically signed.

    I notice that sometimes google have a popup layer on the bottom of layer showing a "welcome back message", but i didn't understand when it appears or if i have to call it manually

    0 讨论(0)
  • 2021-02-08 13:29

    Like the Drew Taylor's answer, to avoid the double callback with the pure javascript sign in solution, you can check the user's session state:

    if (authResult["status"]["method"] == "PROMPT") {...}
    

    I think that the callback with the AUTO method is fired by the bottom welcome bar that appears on first login.

    0 讨论(0)
  • 2021-02-08 13:29

    That is the intentional plan for page level config! It being present in the page causes the callback to fire when the Javascript is finished loading. What you should do is prepare for that in your code.

    Don't show the sign in button until you have received a callback - if authResult['status']['signed_in'] == true, then treat the user as signed in (setup a session etc, whatever you would normally do). If it is false, then display the button.

    function signinCallback(authResult) {
      if (authResult['status']['signed_in']) {
        document.getElementById('signinButton').setAttribute('style', 'display: none');
        // Do sign in stuff here!
      } else {
        document.getElementById('signinButton').setAttribute('style', 'display: block');
      }
    }
    

    I would avoid using approval prompt force if you can!

    0 讨论(0)
  • 2021-02-08 13:35

    I'm facing this same issue here, but I'm calling gapi.auth.signIn() via a button click handler. The callback is still called twice. One thing I noticed between the two authResult objects was that authResult.status.method is 'AUTO' in the first call (before the popup window appears) and is 'PROMPT' in the second call after the window is auto-dismissed due to previous authorisation.

    The solution I'm exploring now is to ignore the AUTO instance and only process the PROMPT instance of the callback. Not sure how this will work once I revoke the permissions within Google due to the lack of details in the docs on the 'status' object.

    0 讨论(0)
  • 2021-02-08 13:35

    I am facing the same issue: signin callback called twice in case of user that already granted permission; the local variable approach (initializedGoogleCallback) isn't working for me because it call the callback one time only when the user already granted access, but didn't call it if the user is the new one. After a bit of research (i especially dig in site using the g+ auth) i noticed that all of them use the 'approvalprompt': 'force' and they have the already granted user to reapprove a "Offline Access" policy everytime. Even the google example i followed to setup my app (https://developers.google.com/+/web/signin/javascript-flow) even if it did not mention it, it uses the "force" parameter. For the moment it seems the only solution if you want to use the javascript flow (that mean if you need a personal style signin button)

    0 讨论(0)
  • 2021-02-08 13:38

    Try to register first call in some local variable and then process it

    This quick solution helps me:

    function login() {
      var initializedGoogleCallback = false
      gapi.auth.signIn({
        'callback': function (authResult) {
           if (!initializedGoogleCallback) {
             // after first call other will be ignored
             initializedGoogleCallback = true;
             if (authResult['status']['signed_in']) {
               console.log('Okay');
             } else {
               console.log('Error');
             }
           }
        }
      });
    }
    

    also you can add following code before call gapi.auth.signIn

    window.removeEventListener('load')
    
    0 讨论(0)
提交回复
热议问题