Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firestore规则使用displayName、Android不起作用_Android_Firebase_Google Cloud Firestore_Firebase Authentication_Firebase Security - Fatal编程技术网

Firestore规则使用displayName、Android不起作用

Firestore规则使用displayName、Android不起作用,android,firebase,google-cloud-firestore,firebase-authentication,firebase-security,Android,Firebase,Google Cloud Firestore,Firebase Authentication,Firebase Security,我有这样的firestore规则: match /{document=**} { allow read, write: if request.auth.token.name == "dummyUser"; } auth.signInAnonymously() .addOnCompleteListener(this) { task -> if (task.isSuccessful) {

我有这样的firestore规则:

 match /{document=**} {

      allow read, write: if request.auth.token.name == "dummyUser";

}
auth.signInAnonymously()
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // Sign in success, update UI with the signed-in user's information
                    Log.d(TAG_MAIN, "signInAnonymously:success")
                    val user = auth.currentUser

                    setUsersSecureName(){
                        makeRequest()
                    }
现在,当我创建UserWithEmailandPassword时,在创建用户之后,我使用我创建的方法setUsersSecureName()将displayName设置为“dummyUser”,如下所示:

fun setUsersSecureName(myCallback: (Boolean?) -> Unit) {

val user = FirebaseAuth.getInstance().currentUser

val profileUpdates = UserProfileChangeRequest.Builder()
    .setDisplayName("dummyUser")
    .build()

user?.updateProfile(profileUpdates)
    ?.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            Log.d(TAG_HELPER_METHODS, "Secure user profile updated.")
            myCallback(true)

        }
    }
}

我对匿名登录也这样做。因此,我创建匿名登录,如下所示:

 match /{document=**} {

      allow read, write: if request.auth.token.name == "dummyUser";

}
auth.signInAnonymously()
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // Sign in success, update UI with the signed-in user's information
                    Log.d(TAG_MAIN, "signInAnonymously:success")
                    val user = auth.currentUser

                    setUsersSecureName(){
                        makeRequest()
                    }
如您所见,使用匿名用户成功登录后,我更改了用户名。现在它似乎被改变了,当我检查它时,用户的显示名是“dummyUser”。但是,firestore请求不起作用,我在日志中得到消息:

PERMISSION_DENIED: Missing or insufficient permissions. 
当用户使用电子邮件和密码登录时,这也不起作用。为什么会这样


感谢

安全规则通过在请求时从Firebase身份验证接收用户ID令牌来工作。Firestore SDK会自动执行此操作。你通常不需要做任何特别的事情


但是,在这种特定情况下,更新用户的配置文件后,Firebase身份验证仍保留一个用户ID令牌,该令牌不知道名称的更改。您可能还必须在配置文件成功更新后通过调用通知Firebase Authentication获取新的配置文件,以便强制刷新ID令牌。刷新成功后,您可以尝试Firestore查询,看看它是否有效。也许也行。这两种方法都是异步的,都会返回一个任务,您应该使用该任务来跟踪请求的完成情况。

Edited,不适用于使用电子邮件和密码登录的新用户。谢谢,这正是我需要的!