使用 flutter ,我已经安装了firebase-auth和 firestore只要我对用户没有任何规则,就可以使用 firebase auth 进行身份验证并调用 firestore。
我有一个调用 _handleEmailSignIn
的按钮,我确实得到了一个有效的用户(因为他们在 Firebase Auth DB 中)
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
void _handleEmailSignIn(String email, String password) async {
try {
FirebaseUser user = await _auth.signInWithEmailAndPassword(
email: email, password: password);
print("Email Signed in " + user.uid); // THIS works
} catch (err) {
print("ERROR CAUGHT: " + err.toString());
}
}
然后我有另一个按钮调用此函数以尝试将记录添加到 testing123
集合中。
Future<Null> _helloWorld() async {
try {
await Firestore.instance
.collection('testing123')
.document()
.setData(<String, String>{'message': 'Hello world!'});
print('_initRecord2 DONE');
} catch (err) {
print("ERROR CAUGHT: " + err.toString());
}
}
现在只要我没有关于检查请求用户的任何规则,它就可以工作。这行得通...
service cloud.firestore {
match /databases/{database}/documents {
match /testing123auth/{doc} {
allow read, create
}
}
}
这并没有给出 PERMISSION_DENIED: Missing or enough permissions。
当我想确保我拥有我使用 _handleEmailSignIn
所做的经过身份验证的用户时。
service cloud.firestore {
match /databases/{database}/documents {
match /testing123auth/{doc} {
allow read, create: if request.auth != null;
}
}
}
我怀疑 firestore 请求不包括 firebase 用户。我是要配置 firestore 以包含用户,还是应该作为 firebase 的一部分自动进行?
最佳答案
需要注意的一点是,firebase_core
是将所有服务连接在一起的“胶水”,当您使用 Firebase 身份验证和其他 Firebase 服务时,您需要确保您正在从相同的 firebase 核心应用程序配置中获取实例。
final FirebaseAuth _auth = FirebaseAuth.instance;
如果您使用多个 Firebase 服务,则不应使用上述这种方式。
相反,您应该始终从 FirebaseAuth.fromApp(app)
获取 FirebaseAuth
并使用相同的配置来获取所有其他 Firebase 服务。
FirebaseApp app = await FirebaseApp.configure(
name: 'MyProject',
options: FirebaseOptions(
googleAppID: Platform.isAndroid ? 'x:xxxxxxxxxxxx:android:xxxxxxxxx' : 'x:xxxxxxxxxxxxxx:ios:xxxxxxxxxxx',
gcmSenderID: 'xxxxxxxxxxxxx',
apiKey: 'xxxxxxxxxxxxxxxxxxxxxxx',
projectID: 'project-id',
bundleID: 'project-bundle',
),
);
FirebaseAuth _auth = FirebaseAuth.fromApp(app);
Firestore _firestore = Firestore(app: app);
FirebaseStorage _storage = FirebaseStorage(app: app, storageBucket: 'gs://myproject.appspot.com');
这可确保所有服务都使用相同的应用配置,并且 Firestore 将接收身份验证数据。
关于firebase - Flutter 和 Firestore 在请求中没有用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49376476/