Firebase 登录并使用用户名注册 [英] Firebase login and signup with username
问题描述
我目前正在使用 AndroidHive 的 tutorial 学习如何使用 Firebase,我现在开始很好地理解方法和文档 - 我意识到 Firebase 提供了一个带有电子邮件和密码的注册/登录方法,而这条信息不是存储在我们的数据库中.
I am currently using AndroidHive's tutorial to learn how to use Firebase, I am starting to understand the methods and documentation well now - I realised that Firebase offers a signup/sign-in method with email and password, and that this piece of information is not stored in our database.
我想实现一个系统,当他们注册时,他们提供他们的电子邮件、密码和用户名以及一些其他数据,并且所有这些都被保存到数据库中.这样用户就可以使用他们的电子邮件或用户名登录.我在堆栈上发现这篇文章显然是 回答 我问的这个问题 - 但是,我不完全理解这一点以及它如何与我下面的代码相关联
I would like to implement a system where when they sign up they provide their email,password and a username along with some other data and it all gets saved to the database. So that the user can then signin with their email or username. I found this article on stack which apparently is the answer to this question I am asking - However, I do not fully understand this and how it links with my code below
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(SignupActivity.this, MainActivity.class));
finish();
}
}
});
非常感谢任何帮助
推荐答案
试试这个:
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
generateUser(email, password)
if (!task.isSuccessful()) {
Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(SignupActivity.this, MainActivity.class));
finish();
}
}
});
这是上面调用的方法.
public void generateUser(String username, String password)
{
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference users = database.getReference("users"); //users is a node in your Firebase Database.
User user = new User(username, password); //ObjectClass for Users
users.push().setValue(user);
}
还有:用户类
public class User {
String username;
String password;
public User() {
//Empty Constructor For Firebase
}
public User(String username, String password)
{
this.username = username; //Parameterized for Program-Inhouse objects.
this.password = password;
}
//Getters and Setters
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
这篇关于Firebase 登录并使用用户名注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!