如何使用Firebase通过电子邮件和简单登录进行简单登录密码 [英] How do I use Firebase Simple Login with email & password

查看:168
本文介绍了如何使用Firebase通过电子邮件和简单登录进行简单登录密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase简单登录提供电子邮件/密码选项,我该如何使用它?从创建用户开始,为用户存储数据,并将其记录到日志中。

解决方案

有三个不同的步骤(让我们假设你有jQuery):

1。设置你的回调




$ b

var ref =新的Firebase(https://demo.firebaseio-demo.com);
var authClient = new FirebaseAuthClient(ref,function(error,user){
if(error){
alert(error);
return;
}
if(user){
//用户已经登录
doLogin(user);
} else {
//用户注销
showLoginBox();
}
});

2。用户注册

function showLoginBox(){
...
//执行所有需要显示登录/注册框的DOM操作。 $(#registerButton)。on(click,function(){
var email = $(#email).val();
var password = $( (#error); val();
authClient.createUser(email,password,function(error,user){
if(!error){
doLogin(user);
} else {
alert(error);
}
});
});
}

3。用户登录


$ b

  function showLoginBox(){
...
//执行所有需要显示登录/注册框的DOM操作。 $(#loginButton)。on(click,function(){
authClient.login(password,{
email:$(#email).val (),
password:$(#password)。val(),
rememberMe:$(#rememberCheckbox).val()
});
} );





当登录成功完成时,您在步骤1中注册的呼叫将被呼叫用正确的用户对象,在这一点上,我们调用 doLogin(user)这是一个你必须实现的方法。



用户数据的结构非常简单。它包含以下属性:

email :用户的电子邮件地址
<$

FirebaseAuthClient会自动验证你的firebsae,而不是进一步的行动是必需的。您现在可以在您的安全规则中使用以下内容:
$ b

{
规则:{
users:{
$ userid:{
.read:auth.uid == $ userid,
.write :auth.uid == $ userid
}
}
}
}

这意味着,如果我的用户ID是42,只有我可以在 example.firebaseio-demo.com/users/42 - 当我登录时 - 而没有其他人。



请注意,简单登录不会存储有关用户的其他信息,除了他们的ID和电子邮件。如果要存储有关用户的其他数据,则必须自己执行此操作(可能在 createUser 的成功回调中)。您可以像往常一样将这些数据存储在Firebase中,只需要注意谁可以读取或写入这些数据即可!


Firebase Simple login provides an email/password option, how do I use it? Starting from from creating a user, storing data for that user, to logging them in and out.

解决方案

There are three distinct steps to be performed (let's assume you have jQuery):

1. Set up your callback

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. User registration

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. User login

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()
    });
  });
}

When the login completes successfully, the call you registered in step 1 will be called with the correct user object, at which point we call doLogin(user) which is a method you will have to implement.

The structure of the user data is very simple. It is an object containing the following properties:

email: Email address of the user id: Unique numeric (auto-incrementing) ID for the user

FirebaseAuthClient will automatically authenticate your firebsae for you, not further action is required. You can now use something like the following in your security rules:

{
  "rules": {
    "users": {
      "$userid": {
        ".read": "auth.uid == $userid",
        ".write": "auth.uid == $userid"
      }
    }
  }
}

This means, if my User ID is 42, only I can write or read at example.firebaseio-demo.com/users/42 - when I am logged in - and no-one else.

Note that Simple Login does not store any additional information about the user other than their ID and email. If you want to store additional data about the user, you must do so yourself (probably in the success callback for createUser). You can store this data as you normally would store any data in Firebase - just be careful about who can read or write to this data!

这篇关于如何使用Firebase通过电子邮件和简单登录进行简单登录密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆