Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

javascript - Google Login Hitting Twice?

I'm using Google Login via JS and it appears my code is getting data twice. I'm not sure why this is occurring.

When I click my "Log In with Google" button, it spits out (console.log(result)) data for the user. THEN a prompt occurs asking me to choose an account of mine (I'm logged into several google accounts). When I click the account I'd like, the code then spits out that user data again.

Why is this occurring? It's a problem because where I spit out the data, I'd like to make a ajax call to verify the user and then redirect them. So in essence, it's trying to do this twice -- which is not cool, what if I don't want to login using the credentials google passes back on the first go around?

(function() {
   var po = document.createElement('script');
   po.type = 'text/javascript'; po.async = true;
   po.src = 'https://apis.google.com/js/client:plusone.js';
   var s = document.getElementsByTagName('script')[0];
   s.parentNode.insertBefore(po, s);
 })();

function googleLogin() {
    var additionalParams = {
        'callback': googleCallback
    };

    gapi.auth.signIn(additionalParams);
}

function googleCallback(authResult) {
    if (authResult['status']['signed_in']) {
        gapi.client.load('oauth2', 'v2', function() {
            gapi.client.oauth2.userinfo.get().execute(function(resp) {
                console.log(resp);
            })
        });
    } else {
        console.log('Sign-in state: ' + authResult['error']);
    }
}

Update: If I sign out of all my Google accounts (with the exception of one and only one), the call to google is still duplicated. This time it logs in and I see console.log() outputting data twice. Access tokens are identical.

Update 2: console.log(resp) is outputting twice

Update 3: Just more clarification:

firebug

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are encountering two calls to "console.log(resp);" within your "googleCallback" function because:

The function that you define for your sign-in callback will be called every time that the user's signed in status changes

This quote is taken from the "Monitoring the user's session state" webpage.

As you can see in the article, the authorization result object has three different status "method" values:

  • null
  • PROMPT
  • AUTO

So your callback code is being triggered when the login prompt appears ("PROMPT") and when the "Welcome back" banner appears ("AUTO").

To stop your callback code from dealing with each trigger event you could change your code as follows:

function signinCallback(authResult) {
    if (authResult['status']['signed_in']) {
        // Update the app to reflect a signed in user
        // Hide the sign-in button now that the user is authorized, for example:
        // document.getElementById('signinButton').setAttribute('style', 'display: none');

        if (authResult['status']['method'] == 'PROMPT') {
            console.log(authResult['status']['method']);

            gapi.client.load('oauth2', 'v2', function () {
                gapi.client.oauth2.userinfo.get().execute(function (resp) {
                    console.log(resp);
                })
            });
        }
    } else {
        // Update the app to reflect a signed out user
        // Possible error values:
        //   "user_signed_out" - User is signed-out
        //   "access_denied" - User denied access to your app
        //   "immediate_failed" - Could not automatically log-in the user
        console.log('Sign-in state: ' + authResult['error']);
    }
}

This code will only call the "gapi.client.oauth2.userinfo.get()" function if a user is signed-in and the event which triggered the callback is of type "PROMPT".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...