Google Login Hitting Twice?

霸气de小男生 提交于 2019-11-27 20:39:57

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".

Google always go through status 'PROMPT', but through status 'AUTO' just when the user has a previous success login and he could be automatically log in.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!