Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sign in API - How do I log someone out with PHP?

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?

like image 387
krummens Avatar asked Mar 09 '16 05:03

krummens


1 Answers

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>
like image 191
Daniel Avatar answered Sep 30 '22 17:09

Daniel