javascript - 如何通过 Google 身份验证在 Firebase Firestore 数据库中创建记录

标签 javascript firebase authentication google-cloud-firestore firebase-authentication

我正在使用 vanilla JS 和 firebase 构建一个 Web 应用程序。我遵循了一个教程,该教程用于用户使用电子邮件和密码注册,并具有创建用户时的代码,将记录插入到“用户”数据库集合中,并从表单中检索用户 ID 和生物值.

这是:

// sign up the user & add firestore data
auth.createUserWithEmailAndPassword(email, password).then(cred => {
    return db.collection('users').doc(cred.user.uid).set({
      bio: signupForm['signup-bio'].value
    });
  }).then(() => {
  //do some other stuff
  });
});

我遇到的问题是我正在尝试在我的谷歌身份验证中实现相同的功能,我希望当用户使用谷歌登录时能够在“用户”数据库集合中创建一个条目。我目前拥有的代码是:

 /**
 * Start the auth flow and authorises to Firebase.
 * @param{boolean} interactive True if the OAuth flow should request with an interactive mode.
 */
function startAuth(interactive) {
// Request an OAuth token from the Chrome Identity API.
chrome.identity.getAuthToken({interactive: !!interactive}, function(token) {
if (chrome.runtime.lastError && !interactive) {
  console.log('It was not possible to get a token programmatically.');
} else if(chrome.runtime.lastError) {
  console.error(chrome.runtime.lastError);
} else if (token) {
  // Authorize Firebase with the OAuth Access Token.
  var credential = firebase.auth.GoogleAuthProvider.credential(null, token);
  firebase.auth().signInWithCredential(credential).catch(function(error) {
    // The OAuth token might have been invalidated. Lets' remove it from cache.
    if (error.code === 'auth/invalid-credential') {
      chrome.identity.removeCachedAuthToken({token: token}, function() {
        startAuth(interactive);
      });
    }
  });
  } else {
  console.error('The OAuth Token was null');
  }
});

}

本质上,我想从上面的代码块中获取功能,并应用在用户集合中创建文档的部分,并将其与第二个 block 一起使用。如果有人可以帮助我了解如何从 google auth 返回用户 ID 并将其插入到文档 ID 与用户 ID 相同的集合“用户”中,我将非常感激。

谢谢,

更新:

**// Authorize Firebase with the OAuth Access Token.
  var credential = firebase.auth.GoogleAuthProvider.credential(null, token);
  firebase.auth().signInWithCredential(credential)
  .then(cred => {
      return db.collection('users').doc(cred.user.uid).set({
        bio: 'testing'
      });
  })
  .catch(function(error) {
      // The OAuth token might have been invalidated. Lets' remove it from cache.
      if (error.code === 'auth/invalid-credential') {
        chrome.identity.removeCachedAuthToken({token: token}, 
function() {
          startAuth(interactive);
        });
      }
  });**

最佳答案

类似于 createUserWithEmailAndPassword()方法,signInWithCredential()方法返回一个 Promise,该 Promise 解析为 UserCredential .

所以你应该这样做:

  firebase.auth().signInWithCredential(credential)
  .then(cred => {
      return db.collection('users').doc(cred.user.uid).set({
        bio: signupForm['signup-bio'].value
      });
  })
  .catch(function(error) {
      // The OAuth token might have been invalidated. Lets' remove it from cache.
      if (error.code === 'auth/invalid-credential') {
        chrome.identity.removeCachedAuthToken({token: token}, function() {
          startAuth(interactive);
        });
      }
  });

关于javascript - 如何通过 Google 身份验证在 Firebase Firestore 数据库中创建记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61303247/

相关文章:

javascript - 如何先隐藏 Div 然后在选择选项后显示

javascript - 构建 Electron 应用程序后,Firebase无法进行身份验证

javascript - 获取 Cloud Firestore 数据库集合

java - wowza authontication 使用外部 jar

javascript - 解析主机时可移植 native 客户端权限被拒绝

javascript - react 路由器在不在 route 时显示组件的问题

javascript - .each 或搜索功能,我该如何使用它们?

android - 如何从 Firebase Task<AuthResult> 登录失败中检索错误代码?

javascript - Firebase 我的用户 ID 结构应该包含 "simplelogin"一词吗?

asp.net-mvc-2 - MVC2 C# 根据 ID 限制对 View 的访问