如何在Android应用程序中使用Firebase(BaaS)存储电子邮件和密码时包含用户名? [英] How do you include a username when storing email and password using Firebase (BaaS) in an Android app?

查看:157
本文介绍了如何在Android应用程序中使用Firebase(BaaS)存储电子邮件和密码时包含用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase createUser()方法需要一个电子邮件和密码字段,但是如果我还想让用户使用一个类似于Snapchat,Instagram,StackOverflow等的自定义用户名?有没有办法修改现有的方法来接受该字段,或者我需要手动推送和管理这些信息,如果是这样的话?



这是我第一次尝试存储所需的用户信息:

  Firebase ref =新的Firebase(firebaseURL); 
ref.createUser(email,password,new Firebase.ValueResultHandler< Map< String,Object>>(){
@Override $ b $ public void onSuccess(Map< String,Object> result) {
System.out.println(用uid成功创建用户帐户:+ result.get(uid));

//在
中标记用户Firebase ref = new Firebase(firebaseURL);
ref.authWithPassword(email,password,new Firebase.AuthResultHandler(){
$ b @Override
public void onAuthenticated(AuthData authData){
System.out.println(User ID:+ authData.getUid()+,Provider:+ authData.getProvider());

//保存用户信息
Firebase userRef = new Firebase(firebaseURL +Users /);
User user = new User(user,authData.getUid());
userRef.setValue(user);

这是一个很好的习惯吗?我想用UID存储用户名可能会帮助我在将来处理更改等。执行 updateChildren() push()方法,所以如果这是一个社交媒体应用程序的条目不会被覆盖?



这是我第二次尝试:

$ p $ @Override $ b $ (AuthData authData){
System.out.println(User ID:+ authData.getUid()+,Provider:+ authData.getProvider());

//保存用户信息和用户名
Firebase ref = new Firebase(firebaseURL);
Map< String,String> map = new HashMap< String,String>();
map.put(email,email);
map.put(username,username);
map.put(provider,authData.getProvider());

ref.child(users)。child(authData.getUid())。setValue(map);


解决方案

显示包含三个字段的表单:


$ b


  1. 用户名

  2. 电子邮件地址
  3. 密码

将后两个发送给Firebase的 createUser()方法。然后在完成回调中,将所有信息存储在Firebase数据库中。

  var userName,emailAddress,password; 
$ b $ // TODO:从用户输入的表单中读取这些值

var ref = new Firebase(https://< YOUR-FIREBASE-APP> .firebaseio.com);
ref.createUser({
email:emailAddress,
password:password
},function(error,authData){
if(error){
console.log(错误创建用户:,错误);
} else {
//将用户的个人资料保存到数据库中,以便我们可以列出用户
//在安全和Firebase规则,并显示配置文件
ref.child(users)。child(authData.uid).set({
provider:authData.provider,
name:userName
});
}
});

请参阅将用户数据存储在Firebase文档中以获取更多信息。


The Firebase createUser() method takes an email and password field, but what if I want to also allow the user a custom username similar to Snapchat, Instagram, StackOverflow etc? Is there any way to modify the existing method to accept that field as well or do I need to do push and manage this info manually and if so how?

This is my first attempt at storing the desired user info:

Firebase ref = new Firebase(firebaseURL);
ref.createUser(email, password, new Firebase.ValueResultHandler<Map<String, Object>>() {
@Override
public void onSuccess(Map<String, Object> result) {
System.out.println("Successfully created user account with uid: " + result.get("uid"));

//Sign user in
Firebase ref = new Firebase(firebaseURL);
ref.authWithPassword(email, password, new Firebase.AuthResultHandler() {

@Override
public void onAuthenticated(AuthData authData) {
System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());

//Save user info
Firebase userRef = new Firebase(firebaseURL + "Users/");
User user = new User(username, authData.getUid());
userRef.setValue(user);

Is this good practice? I figured storing the UID with the username may help me in the future handling changes etc. Also, should I be implementing the updateChildren() or push() method so the entries do not get overwritten if this is a social media app?

This is my second attempt:

  @Override
  public void onAuthenticated(AuthData authData) {
  System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());

  //Save user info and username
  Firebase ref = new Firebase(firebaseURL);
  Map<String, String> map = new HashMap<String, String>();
  map.put("email", email);
  map.put("username", username);
  map.put("provider", authData.getProvider());

  ref.child("users").child(authData.getUid()).setValue(map);

解决方案

Show a form with three fields:

  1. Username
  2. Email address
  3. Password

Send the latter two to Firebase's createUser() method. Then in the completion callback for that, store all information in your Firebase database.

var userName, emailAddress, password;

// TODO: read these values from a form where the user entered them

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.createUser({
  email    : emailAddress,
  password : password
}, function(error, authData) {
  if (error) {
    console.log("Error creating user:", error);
  } else {
    // save the user's profile into the database so we can list users,
    // use them in Security and Firebase Rules, and show profiles
    ref.child("users").child(authData.uid).set({
      provider: authData.provider,
      name: userName
    });
  }
});

See this page on storing user data in the Firebase docs for more information.

这篇关于如何在Android应用程序中使用Firebase(BaaS)存储电子邮件和密码时包含用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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