具有 Google 身份的 Firebase 身份验证 (GoogleYOLO)

Firebase Authentication with Google Identity (GoogleYOLO)

是否可以将 Google Identity (GoogleYOLO) 与 Firebase 身份验证网络堆栈结合起来?如果是这样,如何?谢谢!

您可以使用 Firebase Auth 登录 googleyolo,如下所示:

hintPromise.then((credential) => {
  if (credential.idToken) {
    // Initialize firebase Auth credential with Google ID token
    // obtained from googleyolo.
    const cred = firebase.auth.GoogleAuthProvider.credential(credential.idToken);
    // Sign in with
    return firebase.auth().signInWithCredential(cred);
  }
  throw new Error;
}).then((result) => {
  // User signed in.
}).catch((error) => {
  // Handle error.
});

根据@bojeil 的回复,Firebase 的signInWithCredential 函数所需的ID 令牌存在于credential 对象的credential 属性 中。因此,您必须使用 credential.credential 检索令牌,而不是使用 credential.idToken 检索令牌。下面是使用 Firebase V8 的示例函数。

// firebase V8
function handleCredentialResponse(credential) {
  if (credential) {
    const cred = auth.GoogleAuthProvider.credential(credential.credential);

    // Sign in with credential from the Google user.
    return auth().signInWithCredential(cred);
  }
}

credential 参数是从 Google one-tap 函数回调返回的凭据响应。

 google?.accounts.id.initialize({
    client_id: your-google-app-client-id.apps.googleusercontent.com,
    callback: handleCredentialResponse,
 });
 google?.accounts.id.prompt((notification) => {
    console.log(notification);
 });