谷歌登录打两次? [英] Google Login Hitting Twice?

查看:225
本文介绍了谷歌登录打两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过JS使用Google登录,看起来我的代码正在获取两次数据。我不确定为什么会发生这种情况。



当我点击我的使用Google登录按钮时,会为用户吐出(console.log(result))数据。然后出现一个提示,要求我选择一个我的账户(我登录了几个谷歌账户)。当我点击我想要的帐户时,代码再次吐出用户数据。

为什么会发生这种情况?这是一个问题,因为我在哪里吐出数据,我想做一个Ajax调用来验证用户,然后重定向他们。所以从本质上讲,它试图做两次 - 这不是很酷,如果我不想使用Google第一次回传的凭证登录,该怎么办?

 (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);
})();

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

gapi.auth.signIn(additionalParams);
}

函数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']);
}
}

更新:注销我的所有Google帐户(除了唯一一个帐户外),对Google的呼叫仍然重复。这一次它登录,我看到console.log()两次输出数据。访问令牌是相同的。

更新2: console.log(resp)输出两次

更新3:更多说明:

解决方案

您遇到两次调用console.log(resp);在您的googleCallback函数中,因为:


您为登录回调定义的函数每次被称为
用户的登录状态发生变化


此引用来自监控用户的会话状态网页



正如您所见在文章中,授权结果对象具有三种不同的状态方法值:



  • 提示

  • 自动



因此,当登录提示符出现时, PROMPT)以及出现欢迎回来标题(AUTO)。



要停止回调代码处理每个触发事件,可以更改代码如下所示:

 函数signinCallback(authResult){
if(authResu lt ['status'] ['signed_in']){
//更新应用程序以反映登录用户
//现在隐藏登录按钮,授权用户,例如:
// 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 {
//更新应用程序以反映注销用户
//可能的错误值:
//user_signed_out - 用户已签名-out
//access_denied - 拒绝用户访问您的应用程序
//immediate_failed - 无法自动登录用户
console.log('登录状态: '+ authResult ['error']);






这段代码只会调用gapi.client。 oauth2.userinfo.get()函数,如果用户登录并触发回调的事件类型为PROMPT。


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:

解决方案

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

这篇关于谷歌登录打两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆