Firebase数据库,使auth uid文档的默认ID [英] Firebase database, make auth uid the default id of a document

查看:200
本文介绍了Firebase数据库,使auth uid文档的默认ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 auth createUserWithEmailAndPassword(email,通过)是文档的 uid



我有这个代码作为注册函数:

$ $ p $ $ $ $ $ $ .signup = function(user){
auth.createUserWithEmailAndPassword(user.email,user.password)
.then(ok => {
//保存
删除user.password;
//分配uid
user.uid = ok.uid;
//保存到userRef = ... ref()。child('/ accounts')
userRef.push(user)
.then(snap => console.log(snap))
.catch(err => console.log(err));
})
.catch(e => console.log(e.message));



$ b $ p
$ b

上面的代码完成了预期的工作,输出结果在这里





所以数据保存成功,但我想这个值 -KOQOXUGH9Ahh9fukVkg uid 的值,它是 pFNhADogjkZFz7E4osXPY48wJdS2

似乎 -KOQOXUGH9Ahh9fukVkg 正被 firebase 自动。我如何控制?



这是我的规则

 < 
rules:{
.read:auth!= null,
.write:auth!= null,
accounts:{
.write:auth!= null,
.read:auth!= null,
$ uid:{
.write:$ uid === auth.uid,
.read:$ uid === auth.uid
}
}
}
}


解决方案



  firebase.database()。ref(accounts /+ ok.uid).set(user); 


How can I make the generated uid from auth's createUserWithEmailAndPassword(email, pass) be the uid of document?

I have this code as a signup function :

$scope.signup = function (user) {
    auth.createUserWithEmailAndPassword(user.email, user.password)
        .then(ok => {
            // remove pass before saving
            delete user.password; 
            // assigning the uid
            user.uid = ok.uid;
            // saving to userRef = ...ref().child('/accounts')
            userRef.push(user)
                .then(snap => console.log(snap))
                .catch(err => console.log(err));
        })
        .catch(e => console.log(e.message));
}

The code above does what is expected, the output is here

So data was saved successfully but I want the value of this -KOQOXUGH9Ahh9fukVkg to be the value of the uid which is pFNhADogjkZFz7E4osXPY48wJdS2.

It seems that the -KOQOXUGH9Ahh9fukVkg is being added by firebase automatically. How do I control that?

Here's my rules

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
        "accounts": {
      ".write": "auth != null",
      ".read": "auth != null",
      "$uid": {
        ".write": "$uid === auth.uid",
        ".read": "$uid === auth.uid"
      }
    }
  }
}

解决方案

This should work:

firebase.database().ref("accounts/" + ok.uid).set(user);

这篇关于Firebase数据库,使auth uid文档的默认ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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