如何在Firebase中隐藏信息?

时间:2014-09-14 22:13:04

标签: firebase rules angularfire firebase-security firebasesimplelogin

我有这些要求:

  1. 用户将通过电子邮件和多个Oauth提供商登录。我使用像User.findByEmail('1@1.com')这样的函数。因此,在进行身份验证之前,我需要通过permision查看用户列表。
  2. 用户的电子邮件地址,地理位置和年龄应与其他用户秘密保管。
  3. 我的第一个计划是:

    users:{
      $user-id:{
    
    //  public
        ".read" : true,
        name,
        created_at,
    
        private: {
            ".read" : "auth.email === data.child('email').val()",
            age,
            email,
            geolocation,
        }
        $piority = email
      }
    }
    

    然后我意识到不,它只是不起作用。那么有人请告诉我如何正确地做到这一点?提前谢谢。

    P.S。 Firebase确实需要过滤/序列化方法。

1 个答案:

答案 0 :(得分:4)

这篇文章中确实有几个问题。第一个是如何存储私人数据。您可以做的一个简单的更改是反转公钥/私钥和用户密钥 - 它们不必嵌套在同一个用户记录中。

/users/$user_id/...public data...
/private/$user_id/...private data...

这使得保护数据变得更加简单。

另一个问题是关于重复电子邮件检测。如果我们假设您在这里使用简单登录,这一切都没有实际意义。您只需尝试创建帐户即可查看是否存在电子邮件地址。如果电子邮件已经注册,则会返回错误。

如果仍然无法解决问题,您仍然可以手动检查,而无需提供用户电子邮件列表。这通常使用索引完成。创建新帐户时,请写下以下内容:

/email_index/$escaped_email/$userid ($userid is the value)

现在,当您想检查电子邮件是否可用时,请执行以下操作:

var ref = new Firebase(URL);
function checkEmail(emailAddress, callback) {
   ref.child('email_index/'+escapeEmail(emailAddress)).once('value', function(snap) {
       callback(snap.val() !== null);
   });
}

function escapeEmail(emailAddress) {
   return (email || '').replace('.', ',');
}

checkEmail(EMAIL_ADDRESS, function(exists) {
   console.log(EMAIL_ADDRESS + (exists? 'does' : 'does not') + ' exist!');
 });

为了防止有人列出这些电子邮件,您可以在您的(非常灵活且非常复杂,甚至是企业应用程序)安全规则中执行以下操作:

"email_index": {
   // no .read rule here means that the data cannot be listed; I have to know the email address to check it
   "$email_address": {
       ".read": true,
       // it can only be claimed once and the value must be my user id
       ".write": "auth.uid === newData.val() && !data.exists()"
   }
}