Home » Php » javascript – Google Sign in API – How do I log someone out with PHP?

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&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. .next()‘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>

Answer:

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

Answer:

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');

Answer:

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().

Answer:

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>