如何使用Firebase使用电子邮件和密码进行简单登录
答
有要执行(假设你有jQuery的)三个不同的步骤:设置你的回调
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();
}
});
2.用户注册
1。
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); } }); }); }
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()
});
});
}
当登录成功完成后,您在步骤1中注册的调用将正确的用户对象调用,在这一点上,我们称之为doLogin(user)
这是一种方法,你将不得不实施。
用户数据的结构非常简单。这是一个包含以下属性的对象:
email
:用户的唯一数字(自动递增)ID
FirebaseAuthClient会自动验证您的firebsae你,而不是采取进一步行动:用户 id
的电子邮件地址是必须的。现在,您可以使用类似的安全规则如下:
{
"rules": {
"users": {
"$userid": {
".read": "auth.uid == $userid",
".write": "auth.uid == $userid"
}
}
}
}
这意味着,如果我的用户ID是42,只有我能写或example.firebaseio-demo.com/users/42
阅读 - 当我在我的实际价格 - 并且没有其他人。
请注意,简单登录不会存储除用户ID和电子邮件以外的其他用户信息。如果要存储有关用户的其他数据,则必须自己执行此操作(可能在createUser
的成功回调中)。您可以存储这些数据,因为您通常会将任何数据存储在Firebase中 - 只需要注意谁可以读取或写入此数据!
答
只要有人到达此线程并寻找一些使用Firebase身份验证的示例应用程序。这里有两个例子
var rootRef = new Firebase('https://docs-sandbox.firebaseio.com/web/uauth');
......
.....
....
http://jsfiddle.net/firebase/a221m6pb/embedded/result,js/
http://www.42id.com/articles/firebase-authentication-and-angular-js/
http://engineering.letsnurture.com/firebase-basic-authentication/ – 2016-07-04 08:41:27