如何在网络应用程序中使用 Firebase 完成用户授权?
How the user authorization is done using Firebase in a webapp?
我有以下使用 Firebase 进行用户授权的代码,但我无法理解这些代码。在代码下方,我提到了我面临的疑惑。请有人澄清一下。
var ref = new Firebase("https://prj_name.firebaseio.com");
var loggedInUser = [];
$(document).ready(function() {
authData=ref.getAuth();
if(authData == null){
//TODO find an elegant way to manage authorization
// window.location = "../index.html";
}else{
ref.child("users").child(authData.uid).on("value", function(snapshot){
$( "span.user-name").html(snapshot.val().displayName);
loggedInUser.displayName = snapshot.val().displayName;
});
}
$("#cPassword").focusout(function() {
validatePassword($("#cPassword"), $("#password"));
});
$(document).on("click", ".clickable-row" ,function(){
window.document.location = $(this).data("href");
});
});
function validatePassword(password, cPassword) {
if (cPassword.val() != password.val()) {
cPassword.css('border-color', 'red');
password.css('border-color', 'red');
return false;
}
return true;
}
所有必要的库(如 firebase)都已包括在内,上面的代码工作得非常好,唯一的问题是我无法理解它。
我的疑惑如下:
- 行
authData=ref.getAuth();
的作用是什么?authData 执行后包含什么?
- 在 else 块中,什么是 value 和 snapshot。我根本不明白这句话。
ref.child("users").child(authData.uid).on("value", function(snapshot)
有人能解开我的疑惑吗?谢谢。
好的:
首先,如果您还没有更新您的 firebase 安全规则,您应该先更新:Firebase security rules guide
ref.getAuth() returns 一个值,如果您还没有被授权,则该值将为空,或者它将包含一个对象,其中包含一些关于用户如何使用的信息已获得授权(自定义令牌、Facebook ID、电子邮件等)
这一行:ref.child("users").child(authData.uid).on("value", function(snapshot)
。在这里,您基本上是从用户集合中请求一些数据:'/users/{some unique id}'。当您从 firebase 请求数据时,一旦数据可供使用,Firebase 就会触发 "value" 回调并使用此回调传递数据(快照)。
firebase 文档非常好,我建议通读整个网络指南。 Firebase web guide
希望我已经为您解决了一些问题!
我有以下使用 Firebase 进行用户授权的代码,但我无法理解这些代码。在代码下方,我提到了我面临的疑惑。请有人澄清一下。
var ref = new Firebase("https://prj_name.firebaseio.com");
var loggedInUser = [];
$(document).ready(function() {
authData=ref.getAuth();
if(authData == null){
//TODO find an elegant way to manage authorization
// window.location = "../index.html";
}else{
ref.child("users").child(authData.uid).on("value", function(snapshot){
$( "span.user-name").html(snapshot.val().displayName);
loggedInUser.displayName = snapshot.val().displayName;
});
}
$("#cPassword").focusout(function() {
validatePassword($("#cPassword"), $("#password"));
});
$(document).on("click", ".clickable-row" ,function(){
window.document.location = $(this).data("href");
});
});
function validatePassword(password, cPassword) {
if (cPassword.val() != password.val()) {
cPassword.css('border-color', 'red');
password.css('border-color', 'red');
return false;
}
return true;
}
所有必要的库(如 firebase)都已包括在内,上面的代码工作得非常好,唯一的问题是我无法理解它。
我的疑惑如下:
- 行
authData=ref.getAuth();
的作用是什么?authData 执行后包含什么? - 在 else 块中,什么是 value 和 snapshot。我根本不明白这句话。
ref.child("users").child(authData.uid).on("value", function(snapshot)
有人能解开我的疑惑吗?谢谢。
好的:
首先,如果您还没有更新您的 firebase 安全规则,您应该先更新:Firebase security rules guide
ref.getAuth() returns 一个值,如果您还没有被授权,则该值将为空,或者它将包含一个对象,其中包含一些关于用户如何使用的信息已获得授权(自定义令牌、Facebook ID、电子邮件等)
这一行:
ref.child("users").child(authData.uid).on("value", function(snapshot)
。在这里,您基本上是从用户集合中请求一些数据:'/users/{some unique id}'。当您从 firebase 请求数据时,一旦数据可供使用,Firebase 就会触发 "value" 回调并使用此回调传递数据(快照)。
firebase 文档非常好,我建议通读整个网络指南。 Firebase web guide
希望我已经为您解决了一些问题!