问题
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