9

On Google's Integrate Google Sign-In page it has the section at the bottom that shows you how to sign the user out using Javascript:

<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>

I have been looking and I can't find a way to sign the user out like this using PHP.

I did find how to sign a user completely out of Google, but I don't want that. I also know I can delete the $_SESSION variable that holds the access code, but that still isn't completely what I want.

Does anyone know how I can log someone out of my Google application using PHP?

Tim Dearborn
  • 1,178
  • 7
  • 18
krummens
  • 837
  • 2
  • 17
  • 38

5 Answers5

6

This should work, I fixed the problem with Mark Guinn's code, which was the fact that the gapi.auth2.init(); method's tasks were not finished executing. .then()'ing it solved the issue.

<?php 
    session_start();
    session_unset();
    session_destroy();
?>
<html>
    <head>
        <meta name="google-signin-client_id" content="YOUR_CLIENT_ID">
    </head>
    <body>
        <script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script>
        <script>
            window.onLoadCallback = function(){
                gapi.load('auth2', function() {
                    gapi.auth2.init().then(function(){
                        var auth2 = gapi.auth2.getAuthInstance();
                        auth2.signOut().then(function () {
                            document.location.href = 'login.php';
                        });
                    });
                });
            };
        </script>
    </body>
</html>
Daniel
  • 1,229
  • 14
  • 24
0

JavaScript is the only way for manipulate cookies for domains different from yours.

Maks3w
  • 6,014
  • 6
  • 37
  • 42
0

If you meant to sign a user out upon auth2.signOut() on server side as well, check out this code (though it's python, you should get the idea).

app.signOut = function() {
  // Get `GoogleAuth` instance
  var auth2 = gapi.auth2.getAuthInstance();
  // Sign-Out
  fetch('/signout', {
    method: 'POST',
    credentials: 'include'
  }).then(function(resp) {
    if (resp.status === 200) {
      auth2.signOut()
      .then(changeProfile);
    } else {
      console.error("couldn't sign out");
    }
  }).catch(function(error) {
    console.error(error);
  });
};

And this one

@app.route('/signout', methods=['POST'])
def signout():
    # Terminate sessions
    session.pop('id', None)

    return make_response('', 200)

It depends on how you structure sessions but you can send ajax request to the server before signOut().

Emzor
  • 1,380
  • 17
  • 28
agektmr
  • 2,144
  • 15
  • 14
0

Check this it will work to you

header('Location:https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=http://www.domain.com');
Santosh Ram Kunjir
  • 1,074
  • 1
  • 12
  • 23
0

Why don't you just have your signout script look something like this:

<?php
session_start();
session_unset();
session_destroy();
?>
<html>
   <head>
     <meta name="google-signin-client_id" content="YOUR_CLIENT_ID">
   </head>
   <body>
     <script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script>
     <script>
       window.onLoadCallback = function(){
         gapi.load('auth2', function() {
           gapi.auth2.init();
           var auth2 = gapi.auth2.getAuthInstance();
           auth2.signOut().then(function () {
             document.location.href = 'login.php';
           });
         });
       };
     </script>
   </body>
 </html>
Mark Guinn
  • 619
  • 3
  • 8
  • an error occurs in the javascript console: `ReferenceError: Can't find variable: gap` – krummens Mar 18 '16 at 23:18
  • Are you loading the google api? And did you try my suggestion of using a window.load or onready handler? – Mark Guinn Mar 19 '16 at 17:58
  • Right, so the api isn't loading. Looks like you need to do something like this: http://stackoverflow.com/questions/31144874/gapi-is-not-defined-google-sign-in-issue-with-gapi-auth2-init (updating my answer as well) – Mark Guinn Mar 21 '16 at 11:38
  • now it's returning `TypeError: undefined is not an object (evaluating 'gapi.auth2.getAuthInstance')` – krummens Mar 22 '16 at 01:28
  • Sorry, I thought you had actually used this library before... See edits to post. Make sure you fill in your client id where it says YOUR_CLIENT_ID. – Mark Guinn Mar 22 '16 at 09:45
  • now I have this error: `This method can only be invoked after the token manager is started.` `cb=gapi.loaded_0:65` – krummens Mar 23 '16 at 02:25