Firebase登录和用户名注册 [英] Firebase login and signup with username
问题描述
我目前使用AndroidHive的教程,了解如何使用Firebase,我现在开始了解方法和文档 - 我意识到Firebase提供了一个包含电子邮件和密码的注册/登录方法,并且这一信息不是存储在我们的数据库中。
我想实现一个系统,当他们注册时,他们提供他们的电子邮件,密码和用户名以及其他一些数据,保存到数据库。以便用户可以用他们的电子邮件或用户名登录。我发现这篇文章的堆栈显然是回答这个问题我问 - 但是,我不完全明白这一点,以及它如何链接我的代码
<$ p $新的OnCompleteListener< AuthResult>(){
@Override
public void onComplete(@){code> auth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(SignupActivity.this, NonNull Task< AuthResult>任务){
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.thi s,MainActivity.class));
finish();
}
}
});
非常感谢任何帮助
auth.createUserWithEmailAndPassword(email,password)
。解决方案
(@NonNull Task< AuthResult>任务){
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); //用户是您的Firebase数据库中的一个节点。
用户用户=新用户(用户名,密码); // ObjectClass for Users
users.push()。setValue(user);
$ b User.class
public class User {
String username;
字符串密码;
$ b $ public User(){
//空构造函数对于Firebase
}
$ b $公共用户(字符串用户名,字符串密码)
{
this.username = username; //为Program-Inhouse对象进行参数化。
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;
}
}
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();
}
}
});
Any help is much appreciated thanks
解决方案 Try this:
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();
}
}
});
This is the method that is being called above.
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);
}
Also:
User.class
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屋!