'Firebase rules don't allow access
So my flutter code works fine until I change my Firebase Realtime Database rules. It does not throw an error in debug console or anything, but the app does not get the data. I attach my rules and the login method.
"rules": {
"$uid": {
".read": "auth.uid == $uid || auth.request.uid == $uid",
".write": "auth.uid == $uid || auth.request.uid == $uid"
}
}
And:
void authUser(String email, String password, bool isLogin,
String username) async {
setState(() {
isLoading = true;
});
UserCredential userCreadencial;
FocusScope.of(context).unfocus();
try {
if (isLogin == false) {
userCreadencial = await firebaseAuth.createUserWithEmailAndPassword(
email: email,
password: password,
);
} else {
userCreadencial = await firebaseAuth.signInWithEmailAndPassword(
email: email,
password: password,
);
}
FirebaseFirestore.instance
.collection('users_info')
.orderBy(userCreadencial.user!.uid, descending: true);
await FirebaseFirestore.instance
.collection('users_info')
.doc(userCreadencial.user!.uid)
.set({
'email': userCreadencial.user!.email,
'username': username,
});
setState(() {
isLoading = false;
});
} on PlatformException catch (err) {
print(err);
var message = 'An error occurred, pelase check your credentials!';
if (err.message != null) {
message = err.message!;
}
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text(message),
backgroundColor: Theme.of(context).errorColor,
),
);
setState(() {
isLoading = false;
});
} catch (e) {
print(e);
setState(() {
isLoading = false;
});
rethrow;
}
}
Solution 1:[1]
Your dart code shows that you use the Firestore database but your rules correspond to the Realtime Database. These two different NoSQL Database services offered by Firebase use different security rules syntax.
You should use the Firestore syntax and edit your rules in the correct place:
- Either via the Firebase console through the "Firestore database" vertical menu item, not the "Realtime database" vertical menu item
- Or by modifying the correct rules file as configured in the
firebase.jsonfile of your Firebase project (which is in on your computer), see below.
For a default project config in the firebase.json as shown below, you should define the Firestore rules in the firestore.rules file.
{
"database": {
"rules": "database.rules.json"
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
