Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent google from automatically triggering data-onsuccess="onSignIn"

I did google connect to let users login using their google account.

What i did was, to create a button

<div class="g-signin2 social_signin" data-onsuccess="onSignIn"></div>

Then created onSignIn()

<script>
  function onSignIn(googleUser) {
    // Useful data for your client-side scripts:
    var profile = googleUser.getBasicProfile();
    console.log("ID: " + profile.getId()); // Don't send this directly to your server!
    console.log("Name: " + profile.getName());
    console.log("Image URL: " + profile.getImageUrl());
    console.log("Email: " + profile.getEmail());

    // The ID token you need to pass to your backend:
    var id_token = googleUser.getAuthResponse().id_token;
    console.log("ID Token: " + id_token);
  };
</script>

Then send that token to PHP server for validation and fetching data. That works fine.

But my problem is that, when a user is logged out from my website, google automatically triggers 'onSignIn' without clicking on the button i created. So when user is logged out, user automatically get logged in again.

Can you help me here to prevent this automatic login? Or can i de-authorize the user?

like image 277
Delbin Thomas Avatar asked Nov 22 '15 18:11

Delbin Thomas


1 Answers

Just add onclick and create global variable clicked (default false) and the variable is true you can call BasicProfile.

<div class="g-signin2 social_signin" onclick="ClickLogin()" data-onsuccess="onSignIn"></div>
<script>
var clicked=false;//Global Variable
function ClickLogin()
{
    clicked=true;
}
function onSignIn(googleUser) {
    if (clicked) {
        profile = googleUser.getBasicProfile();    
    }
};
</script>
like image 171
Elid Garazlic Avatar answered Sep 18 '22 02:09

Elid Garazlic