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?
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>