TerribleCoder TerribleCoder - 4 months ago 121
Android Question

Firebase login and signup with username

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

Answer

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;
    }
}
Comments