从 Firebase 中的 auth 对象获取自定义字段

Fetching custom fields from auth object in Firebase

我正在尝试 invalidate/revoke 客户在不同设备上登录时的身份验证令牌。初始授权令牌是通过我们的服务器而不是 firebase 提供的(但使用相同的密钥,因此也适用于 firebase)。

对于每个用户,我们保存一个关联的密码,该密码作为身份验证令牌的一部分传递,当用户切换设备时 - 我们从服务器发出一个新密码并比较密码以使服务器上的令牌无效。然而,Firebase 连接仍然存在。

我正在尝试为每个用户在 firebase 上存储密码。每次我们在后端更改密码并使用它来使 firebase 令牌无效时,都可以更新它。但是,我无法从 auth 对象中提取密码。有什么想法吗?

这是我的 firebase 安全规则。

 {
    "rules": {

      ".read": root.child('passwords').child(auth.uid).val() == auth.password,
      ".write": root.child('passwords').child(auth.uid).val() == auth.password

   }

}

令人惊讶的是,auth 对象中的自定义字段 none 存在。

您放入自定义 auth 对象的任何内容都将在安全规则中可用,并且将成为在客户端进行身份验证后返回的 authData 的一部分。创建 JWT 时,您必须遵循 Firebase 的特定规则,即 outlined here。重要的部分是 d 密钥包含您想要提供的身份验证数据。下面是有效负载的基本示例。

{
  "v": "0",
  "iat": 1448391639,
  "d": {
    "uid": "user1234",
    "password": "mySecretPassword" 
  }
}

http://jwt.io/ 是快速创建 JWT 进行测试的好工具。如果您将上述负载与您的 Firebase 机密结合使用,您应该会在您的身份验证数据 安全规则中看到相同的负载。

f.authWithCustomToken('eyJhbGciOiJIUz...', function(err, data) {
    if (err) {
        // Handle error
    } else {
        console.log(data.auth); // {uid: "userId1234", password: "mySecretPassword"}
    }
});