firebase - Flutter 和 Firestore 在请求中没有用户信息

标签 firebase dart firebase-authentication google-cloud-firestore flutter

使用 flutter ,我已经安装了firebase-authfirestore只要我对用户没有任何规则,就可以使用 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/

相关文章:

firebase - Firebase在Flutter函数中获取uid的问题

listview - 如何检测 ListView Item 的位置?

android - 如何知道哪个用户正在尝试登录?

javascript - 删除超过 2 小时的 firebase 数据

android - Firebase Crashlytics 没有为发布 apk 上传崩溃

flutter - 如何在Flutter StreamBuilder中使用hasData,hasError和ConnectionState处理snapshot.data

ios - 如何为我的iOS应用创建登录功能,该功能仅允许授权用户(由我确定),并且不包括“注册帐户”功能

firebase - 如何知道 Firebase 中的注册电子邮件是否已验证?

android - 与gradle文件同步时无法解析firebase-measurement-connector-impl

javascript - 将 Firebase 模拟器与 AngularFire 一起使用