2013-03-01 54 views

回答

41

有要执行(假设你有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中 - 只需要注意谁可以读取或写入此数据!

+4

我创建了一个的jsfiddle在此基础上在这里:HTTP:// jsfiddle.net/dirkk0/LAJKX/ – dirkk0 2013-06-06 14:55:16

+2

有无论如何我们可以做电子邮件验证,以防止用户简单使用假电子邮件? – vzhen 2013-12-05 06:19:01

+2

电子邮件验证即将推出,请观看Firebase邮寄名单以获得公告! – Anant 2013-12-11 17:15:07

相关问题