6

Google documents there gapi.auth.signOut() method here: https://developers.google.com/+/web/signin/#sign-out

The catch is that it says you can only call that 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.

This works great on my sign-in page itself, but as soon as I've authenticated a user, there's no need to put the G+ sign-in button on subsequent pages. I just want to have a small 'log out' link in the corner that the user can click to sign-out.

I tried having my log-out link call gapi.auth.signOut() but it does nothing unless I am also showing a useless log-in button. How do I get this to work?

dave mankoff
  • 17,379
  • 7
  • 50
  • 64

1 Answers1

5

The button just has to be in the DOM, it doesn't have to be showing. What seems to work well is:

  1. Create a hidden element and attach the gapi handler with gapi.signin.render
  2. Wait for the immediate_failed error - display the button then
  3. Otherwise, accept the signed in callback, and display the logout button

Then you'll get a callback every time, but only show the button when signed out.

Ian Barber
  • 19,765
  • 3
  • 58
  • 58
  • 1
    Could you please elaborate on this? I followed your instructions so that my site renders the button on every page, but only displays it when the user is signed out. The problem is that when I sign out and then refresh so that I get to a page with a sign-in button on (any page) it automatically signs me in again. How can I render it without signing in the user automatically (if the user have been signed in before)? – fgblomqvist Mar 25 '14 at 20:38
  • 1
    That is what the signout method is for - it writes a local cookie so the callback fires with a user signed out error. Make sure your cookiepolicy is right - also be aware that sign might have issues on localhost if youre using that during sev. – Ian Barber Mar 25 '14 at 22:29
  • I see. Well thanks to that last tip I got it working! It didn't work on my dev server (0.0.0.0:6543) but it worked great on the production one. – fgblomqvist Mar 26 '14 at 22:02
  • This did not work for me only way could get away is through revoke as suggested here https://stackoverflow.com/questions/22086301/gapi-auth-signout-not-working-im-lost – Naga May 26 '17 at 19:11