【问题标题】:How do I link auth users to collection in Firestore?如何将身份验证用户链接到 Firestore 中的集合?
【发布时间】:2019-06-26 09:27:33
【问题描述】:

我正在尝试将用户连接到 Firestore 中的用户集合。我正在使用云功能,但我认为我没有正确实施它。

firebase.auth().createUserWithEmailAndPassword(email, password)
    .then(() => {
        console.log('user created')
        exports.createUserDoc = functions.auth.user().onCreate((user) => {
            console.log("hi")
            const userId = user.uid;

            const account = {
                posts: []
            }
            return admin.firestore().collection("Users").doc(userId).add(account)
        })

但是我的 console.log(hi) 没有出现。我正确地接近这个吗?任何建议都有帮助!

【问题讨论】:

  • 我认为你需要先部署你的云功能。当您创建用户时,该功能会自动触发。您没有在应用程序中实现云功能。 cloud.google.com/functions/docs/quickstart-console
  • 云功能是 Node.js(服务器端)。您必须查看云功能日志才能看到任何 console.log 消息。无论如何,.onAuthStateChanged 是监听用户状态变化的正确方法(不是.onCreate
  • 你解决了吗?我也有类似的问题。

标签: firebase react-native google-cloud-firestore


【解决方案1】:

您可以通过一种简化的方式来执行此操作,每次用户注册时,此功能都会创建一个带有特定参数的 firestore 集合。

signupWithEmail: async (_, { email, password, name }) => {
    var user = firebase.auth().createUserWithEmailAndPassword(email, 
    password).then(cred => {
    return 
    firebase.firestore().collection('USERS').doc(cred.user.uid).set({ 
    email,
    name
      })
    })
    return { user }
    }

【讨论】:

【解决方案2】:

现在我所做的是当用户创建一个帐户时 我会将登录信息记录到数据库中。
文档名称设置为 firebase 为用户提供的用户 UID。 现在您可以简单地使用用户 UID 从数据库中请求数据 成为你的.doc(user.uid)

这是完整的代码。

  var htmlEmail = document.getElementById('email').value;
  var htmlPass = document.getElementById('password').value;
  var htmlUser = document.getElementById('username').value.toLowerCase();
  var auth = firebase.auth();


  var promise = auth.createUserWithEmailAndPassword(htmlEmail, htmlPass);
  // If there is any error stop the process.
  promise.catch(function (error) {
      var errorCode = error.code;
      console.log(`GOT ERROR: ` + errorCode)
      if (errorCode == 'auth/weak-password') return // password to weak. Minimal 6 characters
      if (errorCode == 'auth/email-already-in-use') return // Return a email already in use error
  });

  // When no errors create the account
  promise.then(function () {
      var userUid = auth.currentUser.uid;
      var db = firebase.firestore();

      db.collection('users').doc(userUid).set({
          email: htmlEmail,
          emailVertified: false,
          name: htmlUser,
          online: false,
          onlock: false,
          password: htmlPass
      });

然后,当用户登录时,您可以简单地通过 user.uid 请求数据。

var auth = firebase.auth();
firebase.auth().onAuthStateChanged(function (user) {

    // Lay connection with the database.
    var firestore = firebase.firestore();
    var db = firestore.collection('users').doc(user.uid);

    // Get the user data from the database.
    db.get().then(function (db) {
        // Catch error if exists. 
        promise.catch(function (error) {
            // Return error
        });

        promise.then(function () {
            // continue when success
        });
    });
});

可能会有更好的方法。 (仍在学习自己)。 但这对我有用,而且效果很好。

有两件事要记住!

  1. 我会推荐 Firestore 而不是实时数据库,因为它更快、更安全。
  2. 确保您的数据库规则设置正确,这样任何人都无法查看/泄露您的数据库信息。 (因为您正在记录用户的个人信息)。如果设置不正确,用户将能够查看您的数据库甚至清除所有数据。

希望对你有帮助:)

如果您自己找到更好的方法,请在此处告诉我们。 我们也可以从中学习!

【讨论】:

  • 你在这个片段中放了什么:promise.then(function () {}?我正在尝试做你正在做的事情,但不清楚如何从授权工具中获取uid 与用户集合上的 docID 相同。请您分享更多您为使其正常工作所做的工作。
猜你喜欢
  • 2020-08-18
  • 2021-11-02
  • 2021-08-10
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 2021-10-02
  • 2021-01-06
相关资源
最近更新 更多