20

I have created google single sign on by following steps mentioned in https://developers.google.com/identity/sign-in/web/sign-in

The sign in works like a charm but when i try to integrate sign out as per the article in the link

i get the following javascript error in console

Uncaught TypeError: Cannot read property 'getAuthInstance' of undefined

And my signout function looks like

<script>
    function signOut() {
       var auth2 = gapi.auth2.getAuthInstance();
       auth2.signOut().then(function () {
          console.log('User signed out.');
       });
    }
</script>

and my sign in looks like

function onSignIn(googleUser) {

    var profile = googleUser.getBasicProfile();
    console.log('ID: ' + profile.getId()); 
    console.log('Name: ' + profile.getName());
    console.log('Image URL: ' + profile.getImageUrl());
    console.log('Email: ' + profile.getEmail());

}
Kristijan Iliev
  • 4,901
  • 10
  • 28
  • 47
manu g
  • 201
  • 1
  • 2
  • 3

4 Answers4

45

Are signIn and signOut used on the same page? Div g-signin2 loads and inits gapi.auth2 so it should work as long as those are on the same page.

In case signOut is on separate page, you should manually load and init gapi.auth2 library.

Full example (you have to replace YOUR_CLIENT_ID with your actual client_id):

<html>
<head>
   <meta name="google-signin-client_id" content="YOUR_CLIENT_ID">
</head>
<body>
  <script>
    function signOut() {
      var auth2 = gapi.auth2.getAuthInstance();
      auth2.signOut().then(function () {
        console.log('User signed out.');
      });
    }

    function onLoad() {
      gapi.load('auth2', function() {
        gapi.auth2.init();
      });
    }
  </script>
  <a href="#" onclick="signOut();">Sign out</a>

  <script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
</body>
</html>
Jarosław Gomułka
  • 4,935
  • 18
  • 24
  • 8
    Thank you very much sir, now I could handle this. Just put my signout logic in `gapi.auth2.init().then(() => { /* signout stuff here */ })` – Eric Douglas Feb 23 '17 at 23:30
  • This should be accepted as an answer. One more: if you're using angular, jquery or another third party framework, make sure that `onLoad()` function run. If it isn't then your `signOut` won't work – DennyHiu Oct 11 '17 at 08:51
3

Check: https://developers.google.com/identity/sign-in/web/reference#gapiauth2getauthinstance

and in particular this part:

gapi.auth2.getAuthInstance()

Returns the GoogleAuth object. You must initialize the GoogleAuth object with gapi.auth2.init() before calling this method.

For me, the problem is that I didn't call gapi.auth2.init() first

Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
Leon Wang
  • 81
  • 7
2

easiest way is to add ?onload=onLoad so that your api script becomes

 <script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script> 

<script>
    function signOut() {
      var auth2 = gapi.auth2.getAuthInstance();
      auth2.signOut().then(function () {
        console.log('User signed out.');
      });
    }

    function onLoad() {
      gapi.load('auth2', function() {
        gapi.auth2.init();
      });
    }
  </script>
seng
  • 21
  • 1
1

I had same issue and seems that I found solution for me and I think this should be the case.

you should have method call like that, at the bottom of the script, like me, below:

gapi.load("client", initAuth);

this should be modified like

gapi.load("client:auth2", initAuth);

and this should work (at least worked for me).