如何将身份验证用户链接到Firestore中的集合? [英] How do I link auth users to collection in Firestore?

查看:32
本文介绍了如何将身份验证用户链接到Firestore中的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户连接到Firestore中的用户集合.我正在使用云函数,但是我认为我没有正确实现它.

I'm trying to connect a user to the user collection in firestore. I'm using cloud functions, but I don't think I'm implementing it correctly.

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)没有显示.我是否正确地解决了这个问题?任何建议都会有所帮助!

But my console.log(hi) isn't showing up. Am I approaching this correctly? Any advice helps!

推荐答案

现在我要做的是用户创建帐户时我将登录信息记录到数据库中.
文档名称设置为Firebase为用户提供的用户UID.现在,您可以简单地使用用户UID向数据库请求数据,如下所示:是您的 .doc(user.uid).

Right now what i have done is when a user creates an account i will log the login information into the database.
The document name is set to the user UID that firebase give the user. Now you can simply request the data from the database with the user UID as being your .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上请求数据即可.

Then when the user logs you can simply request the data over the 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
        });
    });
});

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

There could just be there are better ways. (still learning myself). But this does the trick for me and works very well.

有两件事要记住!

  1. 我建议在实时数​​据库上使用Firestore,因为它更快,更安全.
  2. 确保正确设置了数据库规则,以便没有人可以查看/泄漏您的数据库信息.(因为您正在记录用户的个人信息).如果设置不正确,用户将能够查看您的数据库,甚至清除所有数据.

希望它会有所帮助:)

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

If you find a better way yourself please let us know in here. We could learn from that also !

这篇关于如何将身份验证用户链接到Firestore中的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆