简体   繁体   English

在 Vue.js 应用程序上使用 Google (GIS) 登录

[英]Sign In With Google (GIS) on Vue.js Application

Apparently google is discontinuing service for gapi.oauth2 .显然谷歌正在停止为gapi.oauth2提供服务。 I'm trying to use their new Sign in With Google tools but they are very confusing.我正在尝试使用他们新的“使用 Google 登录”工具,但它们非常令人困惑。

Project Structure项目结构
I have a Vue frontend where I need to allow users to sign in with google.我有一个 Vue 前端,我需要允许用户使用谷歌登录。 I then need to use the OIDC server flow to authenticate them on my backend.然后我需要使用 OIDC 服务器流在我的后端对它们进行身份验证。

My file structure is the default structure that the Vue CLI gives you.我的文件结构是 Vue CLI 为您提供的默认结构。

I followed these docs but they don't explain how to actually give the user the opportunity to sign in. Like how do we initiate the whole flow?我关注了这些文档,但他们没有解释如何真正让用户有机会登录。比如我们如何启动整个流程? I thought maybe the flow was initiated by the new Sign in With Google Button but I can not figure out how to get this button to work.我想也许流程是由新的“使用 Google 按钮登录”启动的,但我不知道如何让这个按钮工作。

Here is how I'm trying things now:这是我现在尝试的方式:

In App.vue I have the followingApp.vue我有以下

created() {
    loadGSIClient().then((this.GSILoaded = true));
}

googleAuth.js

export function loadGSIClient() {
  console.log("loading GSI");
  return new Promise((resolve, reject) => {
    const script = document.createElement("script");
    script.src = "https://accounts.google.com/gsi/client";
    script.onload = () => {
      var client = window.google.accounts.oauth2.initCodeClient({
        client_id: process.env.VUE_APP_CLIENT_ID,
        scope: "https://www.googleapis.com/auth/calendar.readonly",
        ux_mode: "redirect",
        redirect_uri:
          "http://localhost:5001/sig-wig/us-central1/handleRedirect",
      });
      resolve(client);
    };
    script.onerror = (message, url, line, column, error) => {
      reject({ message, url, line, column, error });
    };
  });
}

Then, in my sign in file AccessRequest I have然后,在我的登录文件AccessRequest我有

  created() {
    var google = window.google;
    google.accounts.id.initialize({
      client_id: process.env.VUE_APP_CLIENT_ID,
      callback: () => {
        "I'm a callback";
      },
    });
    google.accounts.id.renderButton(
      document.getElementById("buttonDiv"),
      { theme: "outline", size: "large" } // customization attributes
    );
  },

However that setup always throws the error Error in created hook: "TypeError: Cannot read properties of undefined (reading 'accounts')"但是,该设置总是Error in created hook: "TypeError: Cannot read properties of undefined (reading 'accounts')"

So it seems like window.google exists when I'm in App.vue but not in AccessRequest.vue .因此,当我在App.vue但不在AccessRequest.vue中时,似乎window.google存在。 Is there some big misunderstanding I'm having about how all of this is supposed to work?我对所有这些应该如何工作有什么大的误解吗?

Is this "Sign in With Google Button" meant to work with an OIDC Server flow?此“使用 Google 按钮登录”是否适用于 OIDC 服务器流程?

Here is My Code这是我的代码
First, You have to put this code index.html in public folder首先,您必须将此代码 index.html 放在公共文件夹中

<script src="https://accounts.google.com/gsi/client" async defer></script>
<template>
 <div ref="googleLoginBtn" />
</template>
<script>
  export default(){
    mounted() {
      const gClinetId = [Your Client Id]
      window.google.accounts.id.initialize({
        client_id: gClientId,
        callback: this.handleCredentialResponse,
        auto_select: true
      })
      window.google.accounts.id.renderButton(
        this.$refs.googleLoginBtn, {
          text: 'Login',
          size: 'large',
          width: '366', // max width 400
          theme: 'outline' // option : filled_black | outline | filled_blue
        }
      )
    },
    methods: {
      async handleCredentialResponse(response) {
         console.log(response.credential)
         // Put your backend code in here
      }
    }
  }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM