Google 一键登录 (YOLO) + Firebase 身份验证?

Bru*_*ier 6 google-authentication firebase firebase-authentication

我注意到谷歌在因安全问题暂停后重新开放了谷歌一键登录和自动注册。

我一直在尝试了解如何使其与 Firebase Auth 一起使用,有人有任何想法吗?即使对它应该如何工作有一个高层次的看法也会很棒。

到目前为止,我已经成功创建了这个:

https://google-one-tap.brunocrosier.now.sh

到目前为止它做了什么:

  1. 显示一键提示
  2. 允许您点击“以 {name} 身份继续”
  3. 向 /api/google 发出发布请求并按照这些说明验证 ID 令牌
  4. 以 JSON 格式输出数据

我的问题是,如何才能使用 Firebase Auth 来完成这项工作?我可以使用 #4 中输出的数据在 Firebase 中创建 Google 用户吗?

提前致谢!

jpr*_*rio 6

一键式 Google 登录 ( https://developers.google.com/identity/gsi/web )

响应通过One Tap 脚本的回调直接到达您的函数。

火力地堡 8

let credential = firebase.auth.GoogleAuthProvider.credential(response.credential);

firebase.auth().signInWithCredential(credential).then(function(result) {
  let user = result.user
  console.log(user)
});
Run Code Online (Sandbox Code Playgroud)

火力地堡 9

import { getAuth, GoogleAuthProvider, signInWithCredential } from "firebase/auth"
    
const yourAuth = getAuth(yourApp)
const credential = GoogleAuthProvider.credential(response.credential)
signInWithCredential(yourAuth, credential).then(function (result) {
  const user = result.user
  console.log(user)
})
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你。略有不同的是,在 Firebase 版本 9 中,您从 auth 包导入 GoogleAuthProvider 对象和 signInWithCredential 方法。之后,它与谷歌一键完美配合。 (2认同)

say*_*now 0

所以看起来它隐藏在此处的文档中https://firebase.google.com/docs/projects/pwa#sign_in_users_across_devices。TLDR添加GOOGLE_YOLOcredentialHelper

var uiConfig = {
  signInSuccessUrl: 'url-to-redirect-to-on-success',
  authMethod: 'https://accounts.google.com',
  signInOptions: firebase.auth.GoogleAuthProvider.PROVIDER_ID,
  // Enable one-tap sign-in.
  credentialHelper: firebaseui.auth.CredentialHelper.GOOGLE_YOLO
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢你的回答!实际上这不起作用,因为 FirebaseUI 不再支持 YOLO (https://github.com/firebase/firebaseui-web/issues/699)但是我确实设法用一些(再次隐藏)文档解决了这个问题: ) (4认同)
  • 典型的谷歌......你应该发布你的解决方法,以防它对其他人有用 (4认同)