Using Flutter with plugins firebase_auth and firebase_database. Authenticating with email and password. This is code extract for authentication and handling auth change event. After auth success, code inserts user info into realtime database. Everything works fine if database rules are set to read/write true. But, with standard database write rules (auth != null), app throws DatabaseError: Permission denied. It appears that database plugin is not aware of authenticated identity? Any ideas what is wrong?
FirebaseAuth _auth = FirebaseAuth.instance;
user = await _auth.signInWithEmailAndPassword(
email: _emailC.text,
password: _passC.text
);
then:
FirebaseAuth _auth = FirebaseAuth.instance;
StreamSubscription<FirebaseUser> _userChangeEvent;
DatabaseReference _usersDbRef = database.reference().child('users');
_userChangeEvent = _auth.onAuthStateChanged.listen((user) {
setState(() {
_isAuthenticated = user != null && user.isEmailVerified;
});
if (_isAuthenticated && _usersDbRef != null) {
_usersDbRef.child(user.uid).once().then((
DataSnapshot data) {
if (data.value == null) {
_usersDbRef.child(user.uid).set({
'id': user.uid,
'email': user.email,
'display_name': user.displayName
});
}
});
}
});
I already wrote that rules were standard, but since Andre asked to include them here they are: These are the rules when everything works fine:
{
"rules": {
".read": "true || auth != null",
".write": "true || auth != null"
}
}
These are the rules that result with "permission denied":
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}