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

2023-12-20

Using flutter http://flutter.io,我已经安装了firebase 身份验证 https://pub.dartlang.org/packages/firebase_auth and 火库 https://pub.dartlang.org/packages/cloud_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 insufficient 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 from FirebaseAuth.fromApp(app)并使用相同的配置来获取所有其他 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 将接收身份验证数据。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flutter 和 Firestore 请求中没有用户信息 的相关文章