谷歌帐户注销和重定向

问题描述 投票:32回答:8

我使用openid登录用户。(仅限谷歌帐户)。现在我的页面中有一个退出链接,点击后,我希望用户退出谷歌帐户,并将页面重定向到我的主页。这可以吗?

编辑- 更改已接受的答案,因为现在Google允许将[继续]重定向到您想要的任何域。

openid logout account
8个回答
63
投票

我已经解决了这个问题,当用户点击注销链接时调用此函数:

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

当用户单击链接时,浏览器会将用户重定向到注销页面,并且只有在注销完成后,用户才会被重定向到站点“http://www.example.com”。

我希望这可以提供帮助。


19
投票

挑战

请求https://www.google.com/accounts/Logout会将用户注销。 Google有时会向该地址添加一个continueUrl参数,但只有当目标是某个Google网站时,才能成功重定向用户,而不是您自己的。这使得该方法无法使用。

此外,OpenID规范此时不包括全局注销。

还有另一种方式:

建议

在您的网页上包含IFrame,并在注销链接上使用onClick JavaScript事件处理程序将https://www.google.com/accounts/Logout加载到IFrame中。之后(您可能想要检查IFrame是否成功加载),将用户重定向到您自己站点的注销过程。注销后,让该页面重定向到您的主页。

链接可能看起来像这样:

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

您需要实现StartPollingForCompletion()函数来定期检查注销页面是否已加载。使用setTimeout()为民意调查计时,并检查IFrame上的一些属性(我不确定哪些属性可行,因为你在这里跨站点工作)。

另见这些问题:

OpenID. How do you logout

How to add logout feature to an OpenID enabled site?


1
投票

我一直在尝试做同样的事情。仅限谷歌应用程序 - 要注销,请尝试以下两个选项: 1)使用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)使用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>

1
投票

由于我花了大量时间在我的应用程序(这是一个Node / MongoDB服务器,使用大量Google Docs和Google Script API)的google登录/注销问题上,我不得不在这里分享我的结果。

完全注销用户的唯一好方法是使用:

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

如果你使用ira上面给出的解决方案,它会在浏览器的当前选项卡中进行部分注销。这意味着如果用户关闭选项卡并重新打开应用程序,他仍将记录到以前的帐户。

我不得不使用外部窗口,因为从电子邮件谷歌帐户注销后我无法正确地重定向到我的应用程序。如果用户带宽不是太慢,这个解决方案大部分时间都有效,在关闭弹出窗口之前让3秒注销。然后,用户需要使用我的主窗口应用程序中的其他帐户进行登录。

这证实了floccinaucinihilipilification的解决方案,也许上面给出的iframe解决方案应该比我的更好。


0
投票

我只需要做同样的事情,注销用户。不知怎的,接受的答案对我不起作用,我从谷歌得到了一个错误

The page you requested is invalid.

所以我最终把它放到我的页面中:

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

这会成功注销用户。 source


0
投票

这就是我做的事情,不记得我是在做什么还是在某个地方找到它...它就像一个魅力......除了它让你退出所有谷歌网站的事实(我不相信他们不提供正确的方法,但你去。)

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

0
投票

我不知道我做的事情是否正确。但我设法使用这样的代码:

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

0
投票

我使用此代码从Google帐户注销。它会抛出一个错误,但不要担心,它会在错误发生后立即重定向到您的应用程序服务器端注销路由。

<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>
© www.soinside.com 2019 - 2024. All rights reserved.