我与 Firebase 的支持人员进行了交谈,找到了一个临时解决方案和一个真正的解决方案。
真正的解决方案:在所有环境中手动管理用户及其密码,使用 Firebase 作为“数据库”。这基本上就是我试图解决我的问题的原因。使用Firebase 自定义身份验证的决心。
临时解决方案:(我所做的因为我不需要真正解决方案提供的安全性)
- 获取可以识别当前用户的东西。在这里,我什至不问他就可以获得当前用户的电子邮件。
Base64 标识符:
byte[] result = System.Text.Encoding.UTF8.GetBytes(email);
email = Convert.ToBase64String(result);
通过 REST 将所需信息放入、推送、修补到firebaseio.com/Base64
在使用 JavaScript 的用户界面中,使用类似 base64.min.js 的方式在用户处执行相同的过程来读取/写入数据
var ref = new Firebase("https://aFirebase.firebaseio.com");
//Things happen
...
//We register a user
function createUser(email, password){
//Allows us to create a user within firebase
ref.createUser({
email : email,
password : password
}, function(error, userData){
if (error) {
//The creation of the user failed
alert(error);
} else {
//The creation of the user succeeded
console.log("Successfully created user account with uid:", userData.uid);
//We make sure we are at the correct position in our firebase
ref = ref.root().child(base64.encode(email));
//We check if the child exist
if(ref == ref.root()){
//The child doesn't exist
//We have to create it
user = {};
//Set the child with a value for the UID, that will fit with the rules
user[base64.encode(email)] = {uid:userData.uid};
//We set the new child with his value in firebase
ref.set(user);
}else{
//The child exist, we can update his information to go accordingly with our rules
ref.update({uid:userData.uid});
}
//Who wants to register and then not be logged in?
//We can add something upon login if his email is not validated...
login(email, password);
}
}
);
}
现在我们必须更新 Firebase 中的规则:
{
"rules": {
"$uid":{
".read":"!(data.child('uid').exists() == true) || data.child('uid').val() == auth.uid",
".write":"!(data.child('uid').exists() == true) || data.child('uid').val() == auth.uid"
}
}
}
有了这个,应用程序在某种程度上是安全的(只要用户使用将设置规则的 C# 应用程序和 JS 应用程序)。