【问题标题】:Request authentication at firestore rules always null with Unity使用 Unity 在 Firestore 规则中请求身份验证始终为空
【发布时间】:2021-08-07 19:29:22
【问题描述】:

当用户想要获取他的数据时,他需要进行身份验证。但是当他检索数据时,firestore 不允许获取数据。我添加了很多调试,在 Unity 中,用户总是经过身份验证,我得到了这个 Exception

Firebase 正确初始化,这是获取用户数据的代码

Debug.Log(FirebaseAuth.DefaultInstance.CurrentUser.UserID);
FDb.Collection("SavedGames").Document("SavedGames").Collection(FirebaseAuth.DefaultInstance.CurrentUser.UserId).GetSnapshotAsync().ContinueWith(task => {
            if (task.IsCompleted)
            {
                Debug.Log(task.Exception);
            }
        });

这里是 Firestor 规则

match /SavedGames/{documents=**} {
      allow read, write: if isAuth();
    }

function isAuth(){
            return request.auth != null;
        }

调试:

SPufxax5oJQXACF9wqRbBeBX1KG2
System.AggregateException: One or more errors occurred. ---> Firebase.Firestore.FirestoreException: Missing or insufficient permissions.
  at Firebase.Firestore.Internal.Util.FlattenAndThrowException (System.Threading.Tasks.Task completedTask) [0x0003d] in Z:\tmp\tmp.DnKB31Zq3X\firebase\firestore\client\unity\proxy\Util.cs:97 
  at Firebase.Firestore.Internal.Util+<MapResult>c__AnonStorey0`2[T,U].<>m__0 (System.Threading.Tasks.Task`1[TResult] completedTask) [0x00000] in Z:\tmp\tmp.DnKB31Zq3X\firebase\firestore\client\unity\proxy\Util.cs:59 
  at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2[TAntecedentResult,TResult].InnerInvoke () [0x00024] in <695d1cc93cca45069c528c15c9fdd749>:0 
  at System.Threading.Tasks.Task.Execute () [0x00010] in <695d1cc93cca45069c528c15c9fdd749>:0 
   --- End of inner exception stack trace ---
---> (Inner Exception #0) Firebase.Firestore.FirestoreException: Missing or insufficient permissions.
  at Firebase.Firestore.Internal.Util.FlattenAndThrowException (System.Threading.Tasks.Task completedTask) [0x0003d] in Z:\tmp\tmp.DnKB31Zq3X\firebase\firestore\client\unity\proxy\Util.cs:97 
  at Firebase.Firestore.Internal.Util+<MapResult>c__AnonStorey0`2[T,U].<>m__0 (System.Threading.Tasks.Task`1[TResult] completedTask) [0x00000] in Z:\tmp\tmp.DnKB31Zq3X\firebase\firestore\client\unity\proxy\Util.cs:59 
  at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2[TAntecedentResult,TResult].InnerInvoke () [0x00024] in <695d1cc93cca45069c528c15c9fdd749>:0 
  at System.Threading.Tasks.Task.Execute () [0x00010] in <695d1cc93cca45069c528c15c9fdd749>:0 <---

UnityEngine.Debug:Log (object)
FirebaseFirestoreManager/<>c:<ReadData>b__17_1 (System.Threading.Tasks.Task`1<Firebase.Firestore.QuerySnapshot>) (at Assets/Scripts/MirariaId/FirebaseFirestoreManager.cs:116)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()
at System.Threading.Tasks.Task.Execute () [0x00010] in <695d1cc93cca45069c528c15c9fdd749>:0 <---

UnityEngine.Debug:Log (object)
FirebaseFirestoreManager/<>c:<ReadData>b__17_1 (System.Threading.Tasks.Task`1<Firebase.Firestore.QuerySnapshot>) (at Assets/Scripts/MirariaId/FirebaseFirestoreManager.cs:116)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()

我可以写入数据,但我无法检索它。

我也尝试将规则设置为 true 而不是 isAuth() 并且它有效。

我尝试过使用 firebase sdk 7.1.0、7.2.0、8.0.0 和 8.1.0 我使用 Unity 2020.3.14f1(LTS)。

【问题讨论】:

  • 根据我的经验,安全规则在提出此要求时通常是正确的。如果您在访问数据库之前登录FirebaseAuth.DefaultInstance.CurrentUser.UserId,那会显示什么?
  • 您好,感谢您的帮助,正如我之前提到的,用户已正确登录,因此它显示了用户的 ID。
  • 很高兴听到您已经检查过了。您可以编辑您的问题以显示该代码及其输出吗?
  • 已经更新了,我也意识到我可以写数据但我无法检索它
  • 嗯....乍一看一切都很好,所以我不确定出了什么问题。希望其他人发现它。

标签: c# unity3d google-cloud-firestore firebase-authentication firebase-security


【解决方案1】:

我回来了,

我将 sdk 降级到 7.0.2,现在可以正常使用了!

我不知道为什么,但它是正确的。

【讨论】:

    猜你喜欢
    • 2021-08-24
    • 2019-07-15
    • 1970-01-01
    • 2020-07-17
    • 2021-07-23
    • 2018-10-17
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多