【问题标题】:onDisconnect() fails with permission error on unauth()onDisconnect() 因 unauth() 上的权限错误而失败
【发布时间】:2016-03-31 01:56:56
【问题描述】:

我无法让 Firebase 身份验证与匿名用户一起使用。我正在使用 Firechat.js 构建一个聊天应用程序,它附加了一个 onDisconnect() 处理程序来将用户设置为“离线”。

但是,似乎当我调用 firebase.unauth() 时,它会在 onDisconnect() 有机会将用户设置为“离线”之前将用户注销,因此它会因权限错误而失败(这是我的理论)。

日志准确显示了我登录然后注销时的一切情况:

 app.js: using username: John Smith  <<logged in
 app.js: calling firebase.unauth()   <<logged out
 firechat.js: Firechat Warning: Firechat requires an authenticated Firebase reference. Pass an authenticated reference before loading.
 app.js: using username: Anonymous3aa-437b  <<after logout, reauthenticate as anonymous
 firebase.js: FIREBASE WARNING: set at /chat/user-names-online/john smith/-KE9LcpieTwxj_A4sBHz failed: permission_denied

正如您所见,在 Firechat 有机会将前一个用户设置为离线之前,该用户是匿名的。 Here is the onDisconnect() bit in firechat.js

这是我的应用代码:

var $firebase = new Firebase(...);
var $firechat = new Firechat(...);

$firebase.onAuth(function(authData) {
  if (authData) {

     var username = authData[authData.provider].displayName;
     var anonusername = "Anonymous" + authData.uid.substr(10, 8);

     console.log('using username: ' + username || anonusername);

     //set firechat user and resume chatting session (user becomes "online")
     $firechat.setUser(authData.uid, username || anonusername, function() {
        $firechat.resumeSession();
     });

  } else {
     //if not logged in, authenticate anonymously 
     $firebase.authAnonymously(function(error) {
        if (error) {
           console.log(error);
        }
     });
  }
});

这是用户名在线的安全规则:

 "user-names-online": {
      // A mapping of active, online lowercase usernames to sessions and user ids.
      ".read": true,
      "$username": {
        "$sessionId": {
          ".write": "(auth != null) && (!data.exists() || !newData.exists() || data.child('id').val() === auth.uid || auth.provider === 'anonymous')",
          "id": {
            ".validate": "(newData.val() === auth.uid)"
          },
          "name": {
            ".validate": "(newData.isString())"
          }
        }
      }
    },

firechat 似乎是为这种情况而构建的,为什么会失败?

【问题讨论】:

    标签: firebase firebase-security firebase-authentication


    【解决方案1】:

    您似乎混淆了一些事情:

    • 调用unauth() 删除用户的身份验证会话。
      • 它不会断开用户的连接。
      • 确实取消任何需要身份验证的位置侦听器。
    • 您附加到onDisconnect() 的代码一旦检测到客户端已断开连接,就会在 Firebase 服务器上运行。
      • 如果onDisconnect() 处理程序将用户从某种存在系统中删除,其他客户端可以看到该用户消失了。
      • 断开连接的客户端无法立即看到自己断开连接的结果,因为它已经断开连接。
      • 如果客户端想知道何时断开连接,请监控.info/connected 值。

    【讨论】:

      猜你喜欢
      • 2018-09-08
      • 2011-04-12
      • 2017-08-31
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 2016-03-03
      相关资源
      最近更新 更多