如何阻止Google Logon onSuccess()在页面加载时触发? [英] How to Stop Google Logon onSuccess() From Firering On Page Load?

查看:102
本文介绍了如何阻止Google Logon onSuccess()在页面加载时触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google登录并可以正常运行,尽管有点太好了.如果用户登录到Google并打开我的登录页面,则将自动触发onSuccess()函数,而无需用户按下Google登录按钮.这样可以防止他们在没有Google的情况下登录.如何在加载页面时停止onSuccess()的触发,而仅在按下按钮时触发?

I’m using Google Sign-In and have it working, albeit a bit too well. If the user is logged in to Google and opens my logon page the onSuccess() function is automatically triggered without the user pressing the Google logon button. This prevents them from logging in without Google. How can I stop onSuccess() from firing when the page is loaded and only fire when the button is pressed?

要重新创建,请用有效的Google客户ID替换MY_ID,打开此页面,登录并刷新.每次刷新都会在浏览器控制台中显示用户信息.

To recreate, replace MY_ID with a valid Google client ID, open this page, login and refresh. Each refresh will display user info in the browser console.

<html>

<meta name="google-signin-client_id" content="MY_ID.apps.googleusercontent.com">

<script src="https://apis.google.com/js/api:client.js"></script>

<script src="https://apis.google.com/js/platform.js?onload=renderButton" async="true" defer></script>

<script>
function renderButton() 
{
  gapi.signin2.render('googleBtn', 
                      {
                        'scope': 'profile email',
                        'width': 240,
                        'height': 50,
                        'longtitle': true,
                        'theme': 'dark',
                        'onsuccess': onSuccess,
                      } );
}

function onSuccess(googleUser) 
{
    var authResponse = googleUser.getAuthResponse(true);
    var profile = googleUser.getBasicProfile();


    var expDate = new Date(authResponse.expires_at);
    console.log('Expires Date: ' + expDate);

    console.log('Access Token: ' + authResponse.access_token);                      // The Access Token granted.
    console.log('Scope: ' + authResponse.scope);
    console.log('Id Token: ' + authResponse.id_token);
    console.log('Expires In: ' + authResponse.expires_in + " seconds");             // The number of seconds until the Access Token expires.
    console.log('First Issued At: ' + new Date(authResponse.first_issued_at));      // The timestamp at which the user first granted the scopes requested.
    console.log('Expires Date: ' + expDate);


    console.log('ID: ' + profile.getId());                                          // Do not send to your backend! Use an ID token instead.
    console.log('Name: ' + profile.getName());
    console.log('Given Name: ' + profile.getGivenName());
    console.log('Family Name: ' + profile.getFamilyName());
    console.log('Image URL: ' + profile.getImageUrl());
    console.log('Email: ' + profile.getEmail());                                    // This is null if the 'email' scope is not present.
}
</script>


<body>

<div  align="center" id="googleBtn" class="customGPlusSignIn"></div>

</body>

</html>

推荐答案

我发现了另一篇帖子

I found another post here that answered the question. Using that I ended up with:

<script src="https://apis.google.com/js/client:platform.js?onload=googleInit" async="true" defer></script>

<meta name="google-signin-client_id" content="**********.apps..googleusercontent.com">


<script>
function googleInit()
{
    gapi.load('auth2', function() {
        auth2 = gapi.auth2.init({});
        element = document.getElementById('googleBtn');
        auth2.attachClickHandler(element, {}, onSuccess, onFailure);
     });
}


function onSuccess(googleUser)
{
    var authResponse = googleUser.getAuthResponse(true);
    var profile      = googleUser.getBasicProfile();
}


function onFailure(error)
{
}

 </script>

<html><body>

    <div id="googleBtn" style="margin-bottom:30px;" align="center" class="g-signin2" data-width="240" data-height="50" data-longtitle="true" data-theme="dark"></div>

</body></html>

这篇关于如何阻止Google Logon onSuccess()在页面加载时触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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