【发布时间】:2019-11-22 17:27:37
【问题描述】:
我有一个使用 cloud_firestore 插件进行数据访问的 Flutter 项目。用户对应用程序进行身份验证后,我需要做什么才能将其设置为 Firestore 客户端使用的身份验证?例如,我只启用了这些基本规则:
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;
}
}
}
一旦我启用了这些规则,来自我的 Flutter 应用程序的每个请求都开始失败。如果我用他们拥有的小“模拟器”测试 Firestore 规则,它们会按预期工作,因此从 Flutter 应用程序端似乎没有正确设置身份验证。
编辑:添加一些代码示例。
我有使用 Google Auth 的身份验证代码,所以当用户登录时,它看起来像这样:
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;
}
我已验证用户的身份验证正确。
然后,当访问 Firestore 时,类似:
DocumentSnapshot userSnapshot = await Firestore.instance
.collection('users')
.document(userId)
.collection('shoppingLists')
.document(listName)
.get();
我已按照所有指南将 Firebase 和 Firestore 添加到我的应用程序中,但我没有看到任何关于将当前经过身份验证的用户设置为发出 Firestore 请求的用户的具体内容,所以我觉得我是那里缺少一些东西。在进行 Firestore 查询以传入当前用户时,我应该做些什么?
【问题讨论】:
-
您找到解决此问题的方法了吗?我遇到了同样的问题,我很疯狂,我怎么找不到答案,只是没有任何解决方案的问题。
-
看看这个答案(“以防万一......”):stackoverflow.com/questions/52812514/…
-
对我来说一切都很好。当您在 Firestore 中访问引用时,检查“userId”是否具有预期值。 Firestore 规则 UI(左下角)中还有一个非常有用的模拟器,您可以使用它。它会准确地告诉您规则失败的地方。
-
我有完全相同的问题,使用任何规则也会导致权限被拒绝错误。
-
现在是 2020 年 4 月,这样一个基本问题仍然没有答案..
标签: flutter google-cloud-firestore firebase-security