I'm trying to prevent double voting in firebase. My idea was to make a vote object in firebase and then add a user_id boolean every time someone votes. So counterTips>votes>User_id1: true> UserId2: True.
These were my security rules :
{
"rules": {
".read": true,
".write": true,
"CounterTips": {
"votes": {
"$uid": {
".write": "auth.uid != null && auth.uid === $uid"
}
}
}
}
}
This is the way my db is structured
CounterTips
user_id: "rI9m9GEkyKhopAhaDNC8GNWPS943"
vote: 5,
votes:
1) bbz9MLo1u7T7zxugmHA806nMof93: true
I'm trying to push current users id into votes object, but instead am just getting a static string that says user_id: true. How do I push an actual user_id to votes and prevent that user from voting again in the security rules?
upvotePost(key, vote, user_id) {
if (this.state.user_id !=null )
{
CounterTipsref.child("votes/${this.state.user_id}").once("value",snapshot => {
if (snapshot.exists()){
const userData = snapshot.val();
console.log("exists!", userData);
}
});
}
else{
alert(this.state.user_id);
let user_id= this.state.user_id;
let votes = { [user_id]: true};
vote++;
CounterTipsRef.child(key).update({ 'vote': vote, 'votes': votes });
}
}
if (this.state.user_id==null) {
this.props.history.push('/login');
}
}
}