gapi.auth.signOut(); not working I'm lost

jcopeland picture jcopeland · Feb 28, 2014 · Viewed 15.5k times · Source

Below is the code I am using to login with google. I have an element on login.php with id authorize-button. When clicked it logs in just fine.

I have a logout link in my header file. When I click the logout it calls gapi.auth.signOut(); then it destroys session and redirects back to login.php

This happens as far as I can tell but then it just logs the user right back into our site with google. This is a pain as some of our users switch from google to facebook logins.

Thanks in advance for any help.

function handleClientLoad() {
    gapi.client.setApiKey(apiKey);
    window.setTimeout(checkAuth, 1);
}

function checkAuth() {
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
}

function handleAuthResult(authResult) {
    var authorizeButton = document.getElementById('authorize-button');


    if (authResult && !authResult.error) {
        //authorizeButton.style.visibility = 'hidden';
        makeApiCall();
    } else {
        //authorizeButton.style.visibility = '';
        authorizeButton.onclick = handleAuthClick;
    }
}

function handleAuthClick(event) {
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
    return false;
}

function signOut() {
    gapi.auth.signOut();
}


function makeApiCall() {

    gapi.client.load('oauth2', 'v2', function() {
        var request = gapi.client.oauth2.userinfo.get();

        request.execute(function(logResponse) {

            var myJSON = {
                "myFirstName": logResponse.given_name,
                "myLastName": logResponse.family_name,
                "name": logResponse.name,
                "socialEmailAddress": logResponse.email
            };

            gapi.client.load('plus', 'v1', function() {

                var request = gapi.client.plus.people.get({
                    'userId': 'me'
                });
                request.execute(function(logResponse2) {
                    //alert(JSON.stringify(logResponse));
                    myJSON['profilePicture'] = logResponse2.image.url;
                    myJSON['socialId'] = logResponse2.id;
                    //alert(JSON.stringify(myJSON));
                    $.ajax({
                        type: "POST",
                        url: "includes/login-ajax.php",
                        data: "function=googleLogin&data=" + JSON.stringify(myJSON),
                        dataType: "html",
                        success: function(msg) {

                            if (msg == 1) {

                                //window.location = "settings.php";
                            }
                        }
                    });
                });
            });
        });
    });
}

Answer

class picture class · Mar 1, 2014

Make sure you have set your cookie-policy to a value other than none in your sign-in button code. For example:

function handleAuthClick(event) {
  gapi.auth.authorize(
    {
      client_id: clientId, 
      scope: scopes, 
      immediate: false, 
      cookie_policy: 'single_host_origin'
    },
    handleAuthResult);
  return false;
}

Note that sign out will not work if you are running from localhost.