I have login with google on my site which is working correctly except for one thing. If I'm logged into my gmail account in the browser, the login script will automatically log me into my website too. Despite executing log out function successfully, the rendered button still says "Signed In" and then logs me in. Here's how login is setup
<script src="js/script.js"></script>
<script src="https://apis.google.com/js/platform.js?onload=googleLoad" async defer></script>
In my script.js file
function googleLoad() {
gapi.load('auth2', function() {
gapi.auth2.init();
});
};
function googleSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
console.log("Email: " + profile.getEmail());
var id_token = googleUser.getAuthResponse().id_token;
// console.log("ID Token: " + id_token);
var fullName = profile.getName(),
imageUrl = profile.getImageUrl(),
email = profile.getEmail(),
provider = 'Google';
};
function loggedOut(href){
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
document.location = href;
});
};
$('#log-out').on('click', function(e){
e.preventDefault();
var href = $(this).attr('href');
loggedOut(href);
});
When you load the page, the googleSignIn functions get's executed and I log in. Apparently this is the name of the default function that is executed on load. If I change the name of the function, it wouldn't run. That would solve the problem but how do I (when do I) execute the function manually?
Then there's the log out button, which when clicked does console the message and logs me out successfully. But on next page load I would just get logged in again because of googleSignIn. What am I doing wrong?