authenticationgoogle-apilogoutgoogle-api-js-client

Signing out using Google API not working


I followed the instructions in:

https://developers.google.com/identity/sign-in/web/sign-in

Everything works (signing in a user) but I cannot sign out a user. I get the following error :

Uncaught gapi.auth2.ExternallyVisibleError: gapi.auth2 has been initialized with different options

It fails when executing :

auth2 = gapi.auth2.init();

(https://developers.google.com/identity/sign-in/web/sign-in#sign_out_a_user)

I need code examples to sign out the user from my web application and also to sign the user completely from the Google account.


Solution

  • gapi.auth2.init(); was called before by

    <div class="g-signin2">
    

    which uses gapi.auth2. You should call

    auth2 = gapi.auth2.getAuthInstance();
    

    instead of gapi.auth2.init(). Full example:

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