Question

I am using openid to log the user in.(google account only). Now I have a sign out link in my page, which on clicking, I want the user to be logged out of google accounts and the page to be redirected to my home page. can this be done ??

Edit-
Changing the accepted answer because now Google allows redirecting [continuing] to any domain you want.

Was it helpful?

Solution

I have solved this issue calling this function when the user click on the logout link:

var logout = function(){
document.location.href = "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=http://www.example.com";
}

When the user click on the link, the browser redirect the user to the logout page and only when the logout is complete, the user is redirected to the site "http://www.example.com".

I hope this can help.

OTHER TIPS

Challenges

Requesting https://www.google.com/accounts/Logout will log the user out. There is a continueUrl parameter that Google adds to that address sometimes, but it will only succeed to redirect the user when the target is some Google site, and not your own. This makes the approach unusable.

Furthermore the OpenID specification does not include global log out at this moment.

There is another way:

Suggestion

Include an IFrame on your page and use the onClick JavaScript event handler on the logout link to load https://www.google.com/accounts/Logout into the IFrame. After that (you might want to check whether the IFrame loaded succesfully), redirect the user to a logout procedure for your own site. After logging out let that page redirect to your home page.

The link might look a bit like this:

<a href="https://www.google.com/accounts/Logout"
    onclick="myIFrame.location='https://www.google.com/accounts/Logout';StartPollingForCompletion();return false;">
   log out</a>
<iframe id="myIFrame"></iframe>

You need to implement the StartPollingForCompletion() function to periodically check whether the logout page has loaded. use setTimeout() to time the poll, and check for some property on the IFrame (I don't know for sure which ones will work, because you're working cross-site here).

see also these questions:

OpenID. How do you logout

How to add logout feature to an OpenID enabled site?

I have been trying to do the same. For google apps only -
To logout try the following two options:
1) Using i-frame -

<iframe src="https://mail.google.com/a/YOURDOMAIN.IN/?logout&hl=en" width="100%" height="300">
  <p>Your browser does not support iframes.</p>
</iframe>

2) Using Javascript -

<script type="text/javascript">
     window.open('https://mail.google.com/a/YOURDOMAIN.IN/?logout&hl=en','logout_from_google','width=600,height=300,menubar=no,status=no,location=no,toolbar=no,scrollbars=no,top=20,left=20');
</script>

As I've spent a huge amount of time on this google login/logout issue for my app (which is a Node/MongoDB server, using massively Google Docs and Google Script APIs), I had to share my results here..

The only good way to completely logout the user is to use this :

 var newWindow = window.open('https://mail.google.com/mail/?logout&hl=fr','Disconnect from Google','width=100,height=50,menubar=no,status=no,location=no,toolbar=no,scrollbars=no,top=200,left=200');
setTimeout(function(){
    if (newWindow) newWindow.close();
    window.location="auth/google";
},3000);

If you use the solution gave above by ira, it is doing a partial logout it seems for the current tab of the browser. Meaning that if the user close the tab, and reopen the application, he will be still logged to the previous account.

I had to use an external window because I was not able to redirect correctly to my app after logout from email google account. This solution works most of times, if user bandwidth is not too slow, letting 3 seconds to the logout being done before closing the popup. Then user is required to log with another account in my main window app.

This confirms the solution of floccinaucinihilipilification and maybe the iframe solution given above should be better than mine.

I just had to do the same, logout the user. somehow the accepted answer doesn't work for me, I got an error from google

The page you requested is invalid.

so I ended up putting this into my page:

<img src="https://www.google.com/accounts/Logout" />

which successfully logs out the user. source

Here's what I do, don't recall if I made it up or found it somewhere... It works like a charm... other than the fact that it logs out you out of all Google sites (I can't believe they don't provide a proper way to do it, but there you go.)

<script>
function makeFrame(domId,url) { 
    ifrm = document.createElement("IFRAME"); 
    ifrm.setAttribute("src", url);
    ifrm.setAttribute("id", domId);
    ifrm.setAttribute("style", "display:none;");         
    ifrm.style.width = 1+"px"; 
    ifrm.style.height = 1+"px"; 
    document.body.appendChild(ifrm); 
} 

function logOutGoogle(){
    makeFrame('googleLogoutIFrame','https://www.google.com/accounts/Logout');
}
$(window).ready(function() {
    logOutGoogle();
});
</script>

I don't know if the thing I did was right. But I managed to use code like this:

<a class="btn btn-default navbar-btn text-white" id="signOut" href="../home.html" onclick="signOut('https://www.google.com/accounts/Logout');">Sign out</a>
    <script>
      function signOut() {
        var auth2 = gapi.auth2.getAuthInstance();
        auth2.signOut().then(function () {
          console.log('User signed out.');
        });
      }
      firebase.auth().signOut().then(function() {
        // Sign-out successful.
        alert('You have Signed Out. Please Sign In');
      }).catch(function(error) {
        // An error happened.
        alert('An error happened');
      });
    </script>

I use this code to log out from google account. It will throw an error but don't worry, it will be redirected immediately after the error takes place directly to your application server-side signout route.

<a href="https://www.google.com/accounts/Logout" target="myiframe">logout</a>
<iframe name="myiframe" style="display:none" onload="redirect()"></iframe>

<script>
  function redirect() {
    window.location = 'http://your.site.com/signout';
  };
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top