2

I am using Google Auth Login for my application. My SignIn button is on the login page and my logout is a part of my header which is included throughout my application. On clicking on logout button I am not being able to sign the user out of my application

Login.tpl code (Which contains sign in button)

<div align="middle" class="g-signin2" data-onsuccess="onSignIn"></div>
<meta name="google-signin-client_id" content="CLIENT_ID">

I am able to login but not being able to logout.Logout button is a part of the header.tpl which is included in all the files but I am getting the error marked in bold above.

Header.tpl code

<meta name="google-signin-client_id" content="CLIENT_ID">

<a href="/users/auth/login" class = "logout"><i class="fa fa-sign-out pull-right"></i> Log Out</a>

My JS code

    $(".logout").click(function(event) {
event.preventDefault();
gapi.load('auth2', function () {
       var auth2 = gapi.auth2.init({
           client_id: 'CLIENT_ID',
           cookiepolicy: 'single_host_origin'
       });
       auth2.then(function(){
            // this get called right after token manager is started
            auth2.signOut();
            console.log('User signed out.');
       });
    });
window.location = $(this).attr('href');
});

The code is never entering the block in which signout is defined and hence I am not getting anything in the console as well.

However if I run this code in console, the user gets logged out successfully but this isn't working on the application when I am running it on localhost

Any leads would be highly appreciated.

Shivanshu Gupta
  • 248
  • 1
  • 14
  • Possible duplicate of [How to sign out a user after page refresh?](http://stackoverflow.com/questions/35118433/how-to-sign-out-a-user-after-page-refresh) – Linda Lawton - DaImTo Jan 09 '17 at 08:12

2 Answers2

1

I found the solution, it seems that you can only call signOut method after the signinCallback has fired. So far as I can tell, the only way to fire the signinCallback is to put a sign-in button on the page.

Keeping a hidden sign-in button where my logout button is written did the trick for me.

Included this in header.tpl where my logout button is written

    <div style = "display:none" align="middle" class="g-signin2" data-cookiepolicy='single_host_origin' data-onsuccess="onSignIn"></div>
Shivanshu Gupta
  • 248
  • 1
  • 14
1

I was able to get it to work on localhost with the following code:

auth2.signOut().then(function () {
   auth2.disconnect();
   gapi.auth2.getAuthInstance().currentUser.get().reloadAuthResponse();
});

Check out the discussion on this github page

hoekma
  • 793
  • 9
  • 19