Step 1: you have to authorize a certain user. To do so go to :
Develop -> Authentication -> Add User
Step 2: You have to set the rules in Firebase.
Go to Database -> Rules and give an access to a certain user to alter the db.
Here's an example of set of rules for a user to alter "Tokens" field:
{
"rules": {
"Tokens" : {
"$uid": {
".read": true,
".write": "auth.uid === $uid"
}
}
}
}
JavaScript code sample:
var email = "XXX@XXX.XXX; //The user that you authenticated in Firebase
var password = XXXXXXXXXXX; //Password of that user
var uidValue = XXXXXXXXX;
const auth = firebase.auth();
//Sign in
const promise = auth.signInWithEmailAndPassword(email, password);
promise.catch(e => console.log(e.message));
//Add a realtime authentication listener
firebase.auth().onAuthStateChanged(firebaseUser => {
if (firebaseUser) {
const rootRef = firebase.database().ref();
const usersRef = rootRef.child('Tokens');
const uid = uidValue;
const daveRef = usersRef.child(uid);
daveRef.set({
id: 232 //New ID
});
}
else {
console.log("not logged in");
}
});
firebase.auth().signOut();
And Finally, the firebase realtime database has the following Fields:
"Tokens" : {
"[UID value (e.g: 123XXX)]" : {
"id" : "164"
}
}