我正在使用Firebase在网站上工作
安全性是:
{
"rules": {
"users": {
".read": true,
".write": true
}
}
}
因此,每个人都可以添加自己的信息,但是没有人可以访问主体。
但是,当有人现在在控制台中键入以下内容时:
ref = new Firebase("https://xxx.firebaseio.com/users");
ref.createUser({
email: email,
password: password
}, function(error, userData) {});
ref.authWithPassword({
email: email,
password: password
}, function(error, authData) {));
ref.remove();
所有用户数据将被删除。
所有用户都有自己的uid(例如
simplelogin:58
)和storageID(例如-Js18LFoT0SmFi2Iq4GP
)我可以和那些做些什么吗?我确实不希望任何人都能删除我的所有用户数据,但我需要让用户编辑自己的信息,并在需要时删除他们的帐户。
这是我的一些代码:
function register() {
var ref = new Firebase("https://fiery-heat-xxx.firebaseio.com/");
ref.createUser({
email: email,
password: password
}, function(error, userData) {
if (error) {
alert("Error creating user: " + error)
} else {
console.log("Successfully created user account with uid:", userData.uid);
var uid = userData.uid
var usersRef = new Firebase("https://fiery-heat-xxx.firebaseio.com/users/" + uid)
var newUser = usersRef.set({
faveShow1: "",
faveShow2: "",
faveShow3: "",
faveShow4: "",
faveShow5: "",
faveShow6: "",
faveShow7: "",
faveShow8: "",
faveShow9: "",
faveShow10: "",
uid: uid
});
//var key = newUser.key();
//console.log(key)
login();
}
});
}
function login() {
clear();
var ref = new Firebase("https://fiery-heat-xxx.firebaseio.com/");
ref.authWithPassword({
email: email,
password: password
}, function(error, authData) {
if (error) {
alert("Login Failed!" + error);
} else {
console.log("Authenticated successfully with payload:", authData);
thisAuthData = authData.uid;
var usersRef = new Firebase("https://fiery-heat-xxx.firebaseio.com/users/" + thisAuthData);
usersRef.on("value", function(snapshot) {
for (var i = 0; i < 1; i++) {
console.log(snapshot.val())
if (true) {
globalAuthData = snapshot.val();
//globalKey = amount;
var SS = snapshot.val()
show1 = SS.faveShow1;
show2 = SS.faveShow2;
show3 = SS.faveShow3;
show4 = SS.faveShow4;
show5 = SS.faveShow5;
show6 = SS.faveShow6;
show7 = SS.faveShow7;
show8 = SS.faveShow8;
show9 = SS.faveShow9;
show10 = SS.faveShow10;
//...//
}
}
}, function(errorObject) {
alert("The read failed: " + errorObject.code);
});
}
});
}
function removeUser() {
clear();
var ref = new Firebase("https://fiery-heat-xxx.firebaseio.com/");
var refSer = new Firebase("https://fiery-heat-xxx.firebaseio.com/users/" + thisAuthData)
ref.removeUser({
email: email,
password: password
}, function(error) {
if (error === null) {
alert("User removed successfully");
refSer.remove();
logoff();
} else {
console.log("Error removing user:", error);
}
});
}
function edit() {
clear();
var fredNameRef = new Firebase('https://fiery-heat-xxx.firebaseio.com/users/' + thisAuthData);
var onComplete = function(error) {
if (error) {
console.log('Synchronization failed');
} else {
console.log('Synchronization succeeded');
console.log(thisAuthData);
console.log(globalAuthData);
login();
}
};
if (document.getElementById("form1").value != "") {
var show1 = document.getElementById("form1").value;
}
var show2 = document.getElementById("form2").value;
var show3 = document.getElementById("form3").value;
var show4 = document.getElementById("form4").value;
var show5 = document.getElementById("form5").value;
var show6 = document.getElementById("form6").value;
var show7 = document.getElementById("form7").value;
var show8 = document.getElementById("form8").value;
var show9 = document.getElementById("form9").value;
var show10 = document.getElementById("form10").value;
fredNameRef.update({
faveShow1: show1,
faveShow2: show2,
faveShow3: show3,
faveShow4: show4,
faveShow5: show5,
faveShow6: show6,
faveShow7: show7,
faveShow8: show8,
faveShow9: show9,
faveShow10: show10,
}, onComplete);
}
function logoff() {
clear()
var ref = new Firebase('https://fiery-heat-xxx.firebaseio.com/')
ref.unauth();
//...//
}
}
和我的安全规则:
{
"rules": {
"users": {
"$user_id": {
".write": "$user_id === auth.uid"
},
".read": true
}
}
}
但是我现在无法注册或更新...
最佳答案
为确保用户信息只能由该用户编辑,您想使用auth.uid
。
https://www.firebase.com/docs/web/guide/understanding-security.html
最重要的内置变量是auth。这个变量是
用户验证后填充。它包含有关它们的数据
和auth.uid,一个唯一的字母数字标识符,适用于
提供者。 auth变量是许多规则的基础。
{
"rules": {
"users": {
"$user_id": {
".write": "$user_id === auth.uid"
}
}
}
}
为了更清楚一点,
auth.uid
指当前登录的用户,而$user_id
指数据库中的位置。 $
指向$location
规则变量:https://www.firebase.com/docs/security/api/rule/path.html
{ "rules": {
"users": {
"$user": {
".read": "auth.uid === $user",
".write": "auth.uid === $user"
}
}
}
}
当用户通过Firebase应用进行身份验证时,会发生三件事:
在客户端的回调中返回有关用户的信息
设备。这使您可以自定义应用程序的用户体验
该特定用户。
返回的用户信息包含一个uid(a
唯一ID),该ID在所有提供商之间均是唯一的,
并且永远不会更改为经过身份验证的特定用户。 uid是一个
包含要验证的提供程序名称的字符串
,后跟冒号和提供者返回的唯一ID。
应用程序的“安全性”和“ Firebase”中auth变量的值
规则已定义。对于未经身份验证的变量,该变量为null
用户,但对于经过身份验证的用户,它是包含
用户的唯一(auth.uid)以及有关该用户的其他数据。
这使您可以安全地控制每个用户的数据访问。