Firebase 如何为创建操作设置Firestore安全规则

Firebase 如何为创建操作设置Firestore安全规则,firebase,flutter,firebase-authentication,Firebase,Flutter,Firebase Authentication,我正致力于在flifter中开发一个移动应用程序,并使用Firebase作为后端 用户需要注册才能使用我的应用程序,我将在Firestore的user\u profile集合中保存一些用户数据。 由于新文档将添加到user\u profile集合中,我想知道当用户注册时,应该如何设置安全规则来创建新文档 以下是我的注册过程中发生的情况: // Step 1: Register user with email and password FirebaseAuth _auth = FirebaseA

我正致力于在flifter中开发一个移动应用程序,并使用Firebase作为后端

用户需要注册才能使用我的应用程序,我将在Firestore的
user\u profile
集合中保存一些用户数据。 由于新文档将添加到
user\u profile
集合中,我想知道当用户注册时,应该如何设置安全规则来创建新文档

以下是我的注册过程中发生的情况:

// Step 1:  Register user with email and password
FirebaseAuth _auth = FirebaseAuth.instance;
await _auth.createUserWithEmailAndPassword(email: email, password: password);

// Step 2: save user data in user_profile collection
FirebaseFirestore.instance
        .collection('user_profile')
        .doc(user.uid)
        .set({
          'uid': user.uid,
          'email': email,
          'name': name,
          'date': DateTime.now,
        })
由于我在调用
createUserWithEmailAndPassword(..)
函数后将用户数据保存在
user\u profile
集合中(意味着用户现在已通过身份验证),在Firebase中定义创建操作的安全规则是否安全,如下所示?或者我应该以不同的方式设置它以使其以正确的方式安全

// Rules for User Profile data
match /user_profile/{any} { 
  // Applies to writes to non-existent documents
  allow create: if request.auth != null; 
}

根据我的经验,这应该没问题,但是您可以另外检查文档中包含的uid是否对应于经过身份验证的用户

allow create: if request.auth != null && 
    request.auth.uid == request.resource.data.uid;