Google+登录:如何注销-使用(gapi.auth.signOut) [英] Google+ Login: How to logout - using (gapi.auth.signOut)

查看:480
本文介绍了Google+登录:如何注销-使用(gapi.auth.signOut)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在如何使用您的Google+帐户登录的网站上创建退出功能方面,我需要一些帮助.

I need a little help on how to create a sign out function from a website where you have sign in using your Google+ account.

文档指出您应该使用gapi.auth.signOut函数

The documentation states you should use the gapi.auth.signOut function

https://developers.google.com/+/web/signin/退出

但是作为javascript的新手,我似乎没有任何运气.

But being a newbie to javascript I don't seem to have any luck.

这是我的代码-退出功能在底部.我在做什么错了?

This is my code - the sign out function is the part at the bottom. What am I doing wrong?

<script>


function signinCallback(authResult) {
  if (authResult['status']['signed_in']) {
    // Update the app to reflect a signed in user
    // Hide the sign-in button now that the user is authorized, for example:
          document.getElementById('signinButton').setAttribute('style','display: none ');
          document.getElementById('callback').setAttribute('style','height: 100px;background-color: red ');

        var mydiv = document.getElementById("callback");
        var aTag = document.createElement('a');
        aTag.innerHTML = "Sign out";
        aTag.id= "signout";
        mydiv.appendChild(aTag);


/*function checkid(){
    document.getElementById('signout')
    }*/

} else {
    // Update the app to reflect a signed out user
    // Possible error values:
    //   "user_signed_out" - User is signed-out
    //   "access_denied" - User denied access to your app
    //   "immediate_failed" - Could not automatically log in the user
    console.log('Sign-in state: ' + authResult['error']);
  }
}

        document.addEventListener('DOMContentLoaded', function () {
      document.getElementById("signout").addEventListener('click', function(){
                gapi.auth.signOut();
                    });

</script>

推荐答案

如果希望要求您的用户重新输入凭据,则需要撤消对该应用程序的用户身份验证.在我的应用程序中,我像下面的代码片段一样进行了操作.您可能会发现此答案很有用.

If you want your user to be asked to reenter credentials, you will need to revoke the user's authentication to your application. In my application i did it like the snippet below. You might find this answer useful.

var token = gapi.auth.getToken();
if (token) {
  var accessToken = gapi.auth.getToken().access_token;
  if (accessToken) {
    // make http get request towards: 'https://accounts.google.com/o/oauth2/revoke?token=' + accessToken
    // In angular you can do it like this:
    // $http({
    //   method: 'GET',
    //   url: 'https://accounts.google.com/o/oauth2/revoke?token=' + accessToken
    // });
  }
}
gapi.auth.setToken(null);
gapi.auth.signOut();

这篇关于Google+登录:如何注销-使用(gapi.auth.signOut)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆