firebase - 如何优雅地处理 Firebase 权限被拒绝异常

标签 firebase firebase-security

有什么办法可以避免控制台上的 permission_denied 异常转储。我不想在控制台上看到那个异常并且想优雅地处理它。我试过follownig,但它似乎不起作用。我也试过 .onAuth() 但这也不能阻止这种情况的发生。

var ref = new Firebase(<My firebase root url>),
 ref.on("value", function(snapshot) {
     console.log("No errors.");
 }, function(err) {
     console.log("Something is wrong. "+err);
 });

Something is wrong. Error: permission_denied: Client doesn't have permission to access the desired data.
SearchCtrl.js:22 User Not Authenticated..Redirecting to Sign In Page...
ionic.bundle.js:19532 Error: permission_denied: Client doesn't have permission to access the desired data.
    at Error (native)
    at J (http://localhost:8100/lib/firebase/firebase.js:120:48)
    at Object.J (http://localhost:8100/lib/firebase/firebase.js:200:378)
    at http://localhost:8100/lib/firebase/firebase.js:185:3
    at vh.h.Hd (http://localhost:8100/lib/firebase/firebase.js:189:104)
    at jh.Hd (http://localhost:8100/lib/firebase/firebase.js:180:364)
    at bh.jh.Da.uh.t [as tg] (http://localhost:8100/lib/firebase/firebase.js:178:281)
    at eh (http://localhost:8100/lib/firebase/firebase.js:172:464)
    at WebSocket.bh.open.va.onmessage (http://localhost:8100/lib/firebase/firebase.js:171:245)(anonymous function) @ ionic.bundle.js:19532c.$$error @ angularfire.min.js:12(anonymous function) @ angularfire.min.js:12(anonymous function) @ angularfire.min.js:12forEach @ ionic.bundle.js:8248g @ angularfire.min.js:12(anonymous function) @ ionic.bundle.js:24148completeOutstandingRequest @ ionic.bundle.js:12830(anonymous function) @ ionic.bundle.js:13210
ionic.bundle.js:19532 destroy called for FirebaseArray: https://fixmycars.firebaseio.com/customers
ionic.bundle.js:19532 TypeError: Cannot read property '$$error' of null
    at angularfire.min.js:12
    at angularfire.min.js:12
    at Object.forEach (ionic.bundle.js:8248)
    at g (angularfire.min.js:12)
    at ionic.bundle.js:24148
    at completeOutstandingRequest (ionic.bundle.js:12830)
    at ionic.bundle.js:13210(anonymous function) @ ionic.bundle.js:19532$get @ ionic.bundle.js:16482(anonymous function) @ ionic.bundle.js:24151completeOutstandingRequest @ ionic.bundle.js:12830(anonymous function) @ ionic.bundle.js:13210

最佳答案

解析错误和/或创建匹配规则来处理您想要处理的所有错误。将处理程序放在当前结构中的 else 语句中。

var ref = new Firebase(<My firebase root url>),
ref.on("value", function(snapshot) {
    if(snapshot.exists()){
        /*Update dom or kick of async method to mount snapshot*/
    }else{
        app.$.errorToast.text = 'Snapshot does not exist';
        app.$.errorToast.show();
    }
    console.log("Did I forget to handle an error");
}, function(err) {
    /*Compare "err" to your rules and handle as needed here*/
    console.log("Something is wrong. "+err);
}); 

在您的代码中,您尝试完成不允许的操作时,“.on”函数将触发并按照上面单个代码块中的定义进行处理。

向客户端提供良好的解决消息,例如“当前用户不允许访问,获取访问权限联系管理员并请求将用户 ID 添加到权限组 {myRefMeta.permgroup}”。我考虑过的一个选择是维护另一个描述权限的分支,或者在提供安全详细信息的基本节点中添加该分支的公共(public)可访问“描述符”。用户的这些知识和您正在使用的树的描述符将能够在此错误发生之前消除此错误。

如果用户注销并且缓存未清除,您的 ref.on 事件将再次触发。发生这种情况时,您应该注意清除缓存并完成完全重新加载。这可以通过在清除缓存后强制刷新浏览器来实现。

在上面的代码块中,错误在 ref.on 中处理,并且 ref.on 永远不会到达重新挂载快照或更新 DOM 的调用。通过在错误处理程序中调用清理方法,您可以挂载空快照、重置引用并禁用不可用的功能。

对于我的目的来说,最好等到需要用户许可才能请求它,并通过创建便利功能“referenceCheck”,您可以根据需要使用它来启动身份验证和引用创建。这将作为“用例”的初始化方法,并且该用例的清理将对此非常补充。

关于firebase - 如何优雅地处理 Firebase 权限被拒绝异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30022331/

相关文章:

firebase - React Native 使用 .then() 中的 this.props 调用异步函数

swift - Cron 作业与安全规则

java - 为什么我的 Firestore 数据权限被拒绝?

android - 如何在一次查询中获取多个 child 的 ID

android - 通过移动数据的 Firebase 匿名身份验证失败

javascript - 允许 Firebase 中的非注册用户执行特定操作

firebase - 发布 Firestore 规则 : An unknown error occurred

php - 在 webView 中 PHP 登录后发送 Firebase 注册 token

firebase - Firestore 安全规则 : understanding match/databases/{database}/documents

Firebase 实时数据库规则 : Validation is cascading to child. 如何忽略父节点上的 .validate?