Firebase database, make auth uid the default id of a document

南楼画角 提交于 2019-12-08 03:55:03

问题


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"
      }
    }
  }
}

回答1:


This should work:

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


来源:https://stackoverflow.com/questions/38793576/firebase-database-make-auth-uid-the-default-id-of-a-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!