Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google+ Login: How to logout - using (gapi.auth.signOut)

I need a little help on how to create a sign out function from a website where you have sign in using your Google+ account.

The documentation states you should use the gapi.auth.signOut function

https://developers.google.com/+/web/signin/sign-out

But being a newbie to javascript I don't seem to have any luck.

This is my code - the sign out function is the part at the bottom. What am I doing wrong?

<script>


function signinCallback(authResult) {
  if (authResult['status']['signed_in']) {
    // Update the app to reflect a signed in user
    // Hide the sign-in button now that the user is authorized, for example:
          document.getElementById('signinButton').setAttribute('style','display: none ');
          document.getElementById('callback').setAttribute('style','height: 100px;background-color: red ');

        var mydiv = document.getElementById("callback");
        var aTag = document.createElement('a');
        aTag.innerHTML = "Sign out";
        aTag.id= "signout";
        mydiv.appendChild(aTag);


/*function checkid(){
    document.getElementById('signout')
    }*/

} else {
    // Update the app to reflect a signed out user
    // Possible error values:
    //   "user_signed_out" - User is signed-out
    //   "access_denied" - User denied access to your app
    //   "immediate_failed" - Could not automatically log in the user
    console.log('Sign-in state: ' + authResult['error']);
  }
}

        document.addEventListener('DOMContentLoaded', function () {
      document.getElementById("signout").addEventListener('click', function(){
                gapi.auth.signOut();
                    });

</script>
like image 702
Jakob Thuemoes Avatar asked Dec 07 '13 21:12

Jakob Thuemoes


2 Answers

The following command works perfectly for me.

gapi.auth2.getAuthInstance().disconnect();
like image 145
George Avatar answered Sep 25 '22 08:09

George


If you want your user to be asked to reenter credentials, you will need to revoke the user's authentication to your application. In my application i did it like the snippet below. You might find this answer useful.

var token = gapi.auth.getToken();
if (token) {
  var accessToken = gapi.auth.getToken().access_token;
  if (accessToken) {
    // make http get request towards: 'https://accounts.google.com/o/oauth2/revoke?token=' + accessToken
    // In angular you can do it like this:
    // $http({
    //   method: 'GET',
    //   url: 'https://accounts.google.com/o/oauth2/revoke?token=' + accessToken
    // });
  }
}
gapi.auth.setToken(null);
gapi.auth.signOut();
like image 29
frmi Avatar answered Sep 24 '22 08:09

frmi