Javascript 每个用户的Firebase安全性
我在一个网站上工作,使用firebaseJavascript 每个用户的Firebase安全性,javascript,html,security,firebase,firebase-security,Javascript,Html,Security,Firebase,Firebase Security,我在一个网站上工作,使用firebase 担保是: { "rules": { "users": { ".read": true, ".write": true } } } 因此,每个人都可以添加自己的信息,但没有人可以访问主要部分。 但当有人现在在控制台中键入以下内容时: ref = new Firebase("https://xxx.firebaseio.com/users"); ref.createUser({ email:
担保是:
{
"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();
//...//
}
}
但是我现在无法注册或更新…要确保用户的信息只能由该用户编辑,您需要使用
auth.uid
最重要的内置变量是auth。这个变量是
在用户验证后填充。它包含关于它们的数据
和auth.uid,这是一个唯一的字母数字标识符,可跨
提供者。AUTH变量是许多规则的基础。
更清楚地说,auth.uid
指的是当前登录的用户,$user\u id
指的是数据库中的位置。$
指向$location
规则变量:
当用户对Firebase应用程序进行身份验证时,会发生三种情况:
- 有关用户的信息在客户端的回调中返回 装置。这允许您自定义应用程序的用户体验 那个特定的用户
- 返回的用户信息包含uid(a) 唯一ID),保证在所有提供商中都是不同的, 并且对于特定的经过身份验证的用户永不更改。uid是一个 包含要验证的提供程序名称的字符串 使用,后跟冒号和从提供程序返回的唯一id
- 应用程序安全和Firebase中auth变量的值 规则被定义。对于未经身份验证的对象,此变量为null 用户,但对于经过身份验证的用户,它是包含 用户的唯一性(auth.uid)以及可能与该用户有关的其他数据。 这允许您安全地控制每个用户的数据访问
好的,但是如果我使用push,我会得到另一种类型的ID:Js18LFoT0SmFi2Iq4GP格式的ID,其中我还存储了用户的UID和关于用户的其他信息。我该如何制定规则?因此,用户只能编辑该“文件夹”中与UID一起的内容?或者是否有更简单的方法在用户帐户中存储多行数据,用户可以编辑这些数据?当您使用
push()
时,您可以将其添加到列表中。您看到的id是Firebase对新列表成员的引用。如果数据有一个定义的路径,比如用户名,您想使用set()
,您在仪表板中使用模拟器吗?这可以为您提供有关permission\u denied
的详细信息,您希望像这样保存usersRef.set({“Jon Snow”){faveShows:{faveShow1:“打破坏习惯”,faveShow2:“行尸走肉”,faveShow3:“权力游戏”}})代码>
{
"rules": {
"users": {
"$user_id": {
".write": "$user_id === auth.uid"
},
".read": true
}
}
}
{
"rules": {
"users": {
"$user_id": {
".write": "$user_id === auth.uid"
}
}
}
}
{ "rules": {
"users": {
"$user": {
".read": "auth.uid === $user",
".write": "auth.uid === $user"
}
}
}
}