HTML PHP google single sign on signout will throw "Cannot read property 'getAuthInstance' of undefined" HTML PHP google single sign on signout will throw "Cannot read property 'getAuthInstance' of undefined" php php

HTML PHP google single sign on signout will throw "Cannot read property 'getAuthInstance' of undefined"


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>


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


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>