3

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');
Boardy
  • 35,417
  • 104
  • 256
  • 447
  • Hi, what about add this you your code: `document.location.href = "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=http://yourDomain/logoutAction"`; nested of `window.location = "/logout.php/state=not_logged_in";` – Anees Hikmat Abu Hmiad Nov 09 '18 at 22:01
  • Technically that does work but it doesn't follow what you're supposed to do according to the docs. That's actually signing the user out of their Google account completely, not just unlinking/deauthorsing their google account to my web app – Boardy Nov 09 '18 at 22:31
  • Yes, thats true, But if you use my suggestion code isset logout without auto sign in again?, your way is truth `auth2.signOut()` since in google documentation this "sign out of your app without signing out of Google"...so that maybe theres autologin script or session is work...so that it back to login again.... – Anees Hikmat Abu Hmiad Nov 09 '18 at 22:39
  • Using your method doesn't auto login when it goes back to the login page. I'm not specifically making it log in automatically, the Google Sign in button is doing that because auth2.signout() isn't working the google account is still logged into my web app so when it goes back to the login page, it re-authenticates again. As your method completely signs out of Google I don't get logged back in again by the sign in button – Boardy Nov 10 '18 at 00:16

0 Answers0