Flutter and Firestore在请求中没有用户信息 [英] Flutter and Firestore does not have user information in request

查看:52
本文介绍了Flutter and Firestore在请求中没有用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 flutter ,安装了 firebase-auth firestore 软件包,并能够只要我对用户没有任何规则,都可以使用firebase auth进行身份验证并调用firestore。

Using flutter, I have installed the firebase-auth and firestore packages and am able to both authenticate with firebase auth and make a call into firestore as long as I don't have any rules around the user.

我有一个调用<$ c的按钮$ c> _handleEmailSignIn ,我的确得到了有效的用户(因为他们在Firebase Auth DB中)

I have a button that calls _handleEmailSignIn and I do get a valid user back (since they are in the 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 集合中。

I then have another button that calls this function to attempt to add a record into the testing123 collection.

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());
  }
}

现在,只要我没有有关检查请求用户的所有规则。这行得通...

Now this works as long as I don't have any rules around checking the request user. This works...

service cloud.firestore {
  match /databases/{database}/documents {
    match /testing123auth/{doc} {
        allow read, create
    }
  }
}

这不会给出 PERMISSION_DENIED:权限丢失或不足。当我要确保我拥有经过身份验证的用户时与 _handleEmailSignIn

This does not which gives PERMISSION_DENIED: Missing or insufficient permissions. when I want to make sure I have the authenticated user I did with _handleEmailSignIn.

service cloud.firestore {
  match /databases/{database}/documents {
    match /testing123auth/{doc} {
        allow read, create: if request.auth != null;
    }
  }
}

我怀疑Firestore请求不包括Firebase用户。我是要配置Firestore来包括用户,还是应该自动将其作为Firebase的一部分?

I suspect that the firestore request is not including the firebase user. Am I meant to configure firestore to include the user or is this supposed to be automatic as part of firebase?

推荐答案

请注意,记录不足的是 firebase_core 是将所有服务连接在一起的胶水,当您使用Firebase身份验证和其他Firebase服务时,需要确保您从相同的Firebase核心应用配置中获取实例。

One thing to note that's not well documented is that firebase_core is the "Glue" that connects all the services together and when you're using Firebase Authentication and other Firebase services, you need to make sure you're getting instances from the same firebase core app configuration.

final FirebaseAuth _auth = FirebaseAuth.instance;

如果您使用多个Firebase服务,则不应使用上述方式。
相反,您应该始终从 FirebaseAuth.fromApp(app)获取 FirebaseAuth ,并使用相同的配置来获取所有其他Firebase服务。

This way above should not be used if you're using multiple firebase services. Instead, you should always get FirebaseAuth from FirebaseAuth.fromApp(app) and use this same configuration to get all other Firebase services.

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将接收身份验证数据。

This insures that all services are using the same app configuration and Firestore will receive authentication data.

这篇关于Flutter and Firestore在请求中没有用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆