如何停止 Google Logon onSuccess() 在页面加载时触发?
How to Stop Google Logon onSuccess() From Firering On Page Load?
我正在使用 Google 登录并让它正常工作,尽管有点太好了。如果用户登录 Google 并打开我的登录页面,则 onSuccess() 函数会自动触发,而无需用户按下 Google 登录按钮。这可以防止他们在没有 Google 的情况下登录。如何在加载页面时阻止 onSuccess() 触发,并且仅在按下按钮时触发?
要重新创建,请将 MY_ID 替换为有效的 Google 客户端 ID,打开此页面,登录并刷新。每次刷新都会在浏览器控制台中显示用户信息。
<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>
我发现另一个 post 回答了这个问题。使用它我最终得到:
<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 登录并让它正常工作,尽管有点太好了。如果用户登录 Google 并打开我的登录页面,则 onSuccess() 函数会自动触发,而无需用户按下 Google 登录按钮。这可以防止他们在没有 Google 的情况下登录。如何在加载页面时阻止 onSuccess() 触发,并且仅在按下按钮时触发?
要重新创建,请将 MY_ID 替换为有效的 Google 客户端 ID,打开此页面,登录并刷新。每次刷新都会在浏览器控制台中显示用户信息。
<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>
我发现另一个 post
<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>