Ana*_*ant 32 firebase firebase-security
Firebase简单登录提供电子邮件/密码选项,我该如何使用它?从创建用户,存储该用户的数据到登录和注销.
Ana*_*ant 41
有三个不同的步骤要执行(让我们假设您有jQuery):
1.设置回调
var ref = new Firebase("https://demo.firebaseio-demo.com");
var authClient = new FirebaseAuthClient(ref, function(error, user) {
if (error) {
alert(error);
return;
}
if (user) {
// User is already logged in.
doLogin(user);
} else {
// User is logged out.
showLoginBox();
}
});
Run Code Online (Sandbox Code Playgroud)
2.用户注册
function showLoginBox() {
...
// Do whatever DOM operations you need to show the login/registration box.
$("#registerButton").on("click", function() {
var email = $("#email").val();
var password = $("#password").val();
authClient.createUser(email, password, function(error, user) {
if (!error) {
doLogin(user);
} else {
alert(error);
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
3.用户登录
function showLoginBox() {
...
// Do whatever DOM operations you need to show the login/registration box.
$("#loginButton").on("click", function() {
authClient.login("password", {
email: $("#email").val(),
password: $("#password").val(),
rememberMe: $("#rememberCheckbox").val()
});
});
}
Run Code Online (Sandbox Code Playgroud)
登录成功完成后,将使用正确的用户对象调用您在步骤1中注册的呼叫,此时我们将调用doLogin(user)哪个是您必须实现的方法.
用户数据的结构非常简单.它是一个包含以下属性的对象:
email:用户的电子邮件地址:用户的
id唯一数字(自动递增)ID
FirebaseAuthClient会自动为您的firebsae验证,无需采取进一步措施.您现在可以在安全规则中使用以下内容:
{
"rules": {
"users": {
"$userid": {
".read": "auth.uid == $userid",
".write": "auth.uid == $userid"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着,如果我的用户ID是42,那么只有我可以写入或读取example.firebaseio-demo.com/users/42- 当我登录时 - 并且没有其他人.
请注意,简单登录不会存储除用户ID和电子邮件之外的任何有关用户的其他信息.如果要存储有关用户的其他数据,则必须自己进行(可能在成功回调中createUser).您可以像通常在Firebase中存储任何数据一样存储此数据 - 只需注意谁可以读取或写入此数据!
| 归档时间: |
|
| 查看次数: |
31920 次 |
| 最近记录: |