Reputation: 154
I'm trying to write data to the realtime database by first logging in users always using firebase (google_auth). But there is something wrong with my rules ... if I set write and read "true" they obviously go, the same if I just use "auth! = Null" but when I use these rules:
{
"rules": {
"users": {
"$uid": {
".read": "auth! = null && auth.uid == $ uid",
".write": "auth! = null && auth.uid == $ uid"
}
}
}
}
No longer goes. So I assume the problem is that auth.uid is not the same as uid. I am attaching the code used for authentication and data submission.
Login / registration:
Future <UserCredential> signInWithGoogle () async {
final GoogleSignInAccount? userGoogle = await GoogleSignIn (). signIn ();
final GoogleSignInAuthentication? Google user data =
await userGoogle? .authentication;
final credentialsGoogle = GoogleAuthProvider.credential (
accessToken: Google User data? .accessToken,
idToken: Google User data? .idToken,
);
return await FirebaseAuth.instance.signInWithCredential (Google credentials);
}
Data sending:
DatabaseReference ref = FirebaseDatabase.instance.ref ();
await ref.set ({
"users": {
"$uid": {
"name": "John",
}
}
});
uid is fetched like this:
checkLogin () {
FirebaseAuth.instance.authStateChanges (). Listen ((User? User) {
if (user! = null) {
username = user.displayName;
uid = user.uid;
}
});
}
Upvotes: 0
Views: 127
Reputation: 17734
i think it's because of wrong spacing.. edited the rules. Try this please
{
"rules": {
"users": {
"$uid": {
".read": "auth != null && auth.uid == $uid",
".write": "auth != null && auth.uid == $uid"
}
}
}
}
Upvotes: 1