I have a Flutter project that's using the cloud_firestore plugin for data access. Once a user authenticates to the application, what do I need to do to set that as the authentication used by the Firestore client? For example, I just have these basic rules enabled:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, update, delete: if request.auth.uid == userId;
allow create: if request.auth.uid != null;
}
match /users/{userId}/{document=**} {
allow read, update, delete, create: if request.auth.uid == userId;
}
match /ingredients/* {
allow read, create: if request.auth.uid != null;
}
match /units/* {
allow read, create: if request.auth.uid != null;
}
match /recipes/* {
allow read, create, update: if request.auth.uid != null;
}
}
}
As soon as I enabled those rules, every request from my Flutter app started failing. If I test the Firestore rules with the little "simulator" they have, they work as expected, so the authentication does not appear to be getting set correctly from the Flutter app side.
EDIT: Adding some code samples.
I have authentication code that uses Google Auth, so when the user logs in it looks like this:
class Auth implements AuthService {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: [
'email',
'https://www.googleapis.com/auth/contacts.readonly',
],
);
Future<String> signInWithGoogle() async {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
final FirebaseUser user = await _firebaseAuth.signInWithCredential(credential);
return user.uid;
}
I've verified that the user is being authenticated properly.
Then, when accessing Firestore, something like:
DocumentSnapshot userSnapshot = await Firestore.instance
.collection('users')
.document(userId)
.collection('shoppingLists')
.document(listName)
.get();
I've followed all of the guides to add Firebase and Firestore to my app, and I didn't see anything specific about setting the currently authenticated user as the user that's making the Firestore requests, so I feel like I'm missing something there. Is there something I'm supposed to be doing when making the Firestore queries to pass in the current user?