I am working on a project where the user can sign in using their Google account. Once they sign in, they are taken to another page, and the user can browse between multiple different pages, with each page having a log out button.
I am trying to implement it so that if the user clicks log out, and they used Google sign in, it signs their Google account out from the app but I keep getting some weird error.
Each page has the meta tag that includes my client id as follows:
<meta name="google-signin-client_id" content="OMITTED-CLIENT-ID.apps.googleusercontent.com">
And each page includes the platform.js as below:
<script src="https://apis.google.com/js/platform.js" async defer></script>
When the logout is clicked, I check if the user used Google Signin and if so initialise the auth2 and try and signout as follows:
if (getCookie("UsingGoogleSignIn") === "1")
{
gapi.load('auth2', function() {
gapi.auth2.init();
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function(){
alert("Successfully Logged Out");
//window.location = "/logout.php/state=not_logged_in";
});
});
}
The error I am getting back is
Uncaught Error: nb
at tE (cb=gapi.loaded_0:192)
at jF.<anonymous> (cb=gapi.loaded_0:237)
at new _.C (cb=gapi.loaded_0:111)
at jF.BT (cb=gapi.loaded_0:237)
at Ay.Qv.a.(anonymous function) [as signOut] (https://apis.google.com/_/scs/apps-static/_/js/k=oz.gapi.en_GB.GSouU9gH1pU.O/m=auth2/rt=j/sv=1/d=1/ed=1/am=QQ/rs=AGLTcCNXlVeYtOxUtk6wkVB3WmvUfICGTA/cb=gapi.loaded_0:218:203)
at HelperFunctions.js:195
at platform.js:18
at Oa (platform.js:10)
at b (platform.js:18)
at Array.E.(anonymous function) (https://apis.google.com/js/platform.js:18:292)
In the stack the line HelperFunctions.js:195 is the following line in my code:
auth2.signOut().then(function(){
UPDATE
I partly figured out the issue in the sense that I should have used the promise in the init function to then trigger the signout, so my code now looks like the following:
gapi.load('auth2', function() {
gapi.auth2.init().then(function(){
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function(){
window.location = "/logout.php/state=not_logged_in";
});
});
});
This now does appear to work and it redirects to /logout.php but Google doesn't seem to get signed out. When logout.php is done, it redirects back to login.php and then that automatically signs back into my web app using the Google Account.
For reference the PHP logout.php contains the following:
session_start();
unset($_COOKIE["ClientID"]);
setcookie("ClientID", null, -1, '/');
unset($_SESSION["ClientID"]);
unset($_SESSION["AuthToken"]);
unset($_SESSION["UserID"]);
session_destroy();
header('Location: /login.php?state=not_logged_in');