【问题标题】:How to implement sign out via gapi如何通过 gapi 实现注销
【发布时间】:2021-11-08 12:09:40
【问题描述】:

我正在编写 Vue js 应用程序。

在 index html 文件中,我在 head 标签中包含以下内容:

<meta name="google-signin-client_id" content="<my_client_id>.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>

在编写登录组件时,我使用以下代码(这个link 帮助了我)来实现登录按钮:

template: `
...

    <div id="google-signin-button"></div>
...
`

methods: {
...
        onSignIn (user) {
          const profile = user.getBasicProfile();
          this.login(profile);
        }
...
}

mounted() {
...
      gapi.signin2.render('google-signin-button', {
        onsuccess: this.onSignIn
      })
...
    },

它奏效了。现在我尝试在导航栏组件(重定向后是其他组件的一部分)中使用以下代码来实现注销,但我得到“TypeError: Cannot read properties of undefined (reading 'getAuthInstance')”:

logout: function () {
  var self = this;
    if(confirm("Вы действительно хотите выйти?")) {
      const auth2 = gapi.auth2.getAuthInstance();

      auth2.signOut().then(function () {
          console.log('user signed out');
          self.$store.dispatch('authentication/logout')
          .then(() => {
            self.$router.push('/login')
          })
      });
    }
  },

我尝试了 link 的答案,但我收到 cookie 策略错误,不知道该怎么办。可能有其他方法吗?请帮忙!如果你愿意,请纠正我的语法错误。谢谢!

【问题讨论】:

    标签: vue.js google-signin logout google-login google-api-js-client


    【解决方案1】:

    好的,我忘记运行网络服务器了。然后我使用了最后一个链接,获取 auth2 全局变量,并使用它来签名。完成。

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2011-10-29
      • 2015-06-26
      • 2014-05-26
      相关资源
      最近更新 更多