I currently started using Firebase for my Android application and I used the samples on their GitHub to create the login and register activity.
https://github.com/firebase/quickstart-android
The only thing is that I noticed that their version only has an email and password for both login and registration. I would like to add a username field at the time of registration for my users. I did find some other links that might provide an answer but I know that Firebase was recently changed and those answers didn't show the same method of registration as the example so I figured it might be different. This is my createAccount code block:
public void createAccount(String email, String password) {
if (!validateForm()) {
return;
}
showProgressDialog();
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(LoginActivity.this, "An error occurred", Toast.LENGTH_SHORT).show();
}
hideProgressDialog();
}
)};
}
What createUserWithEmailAndPassword
creates is an internal firebase user only used for authentication (you can see this info in Auth in the new console). If you want to add more information to this user, like Sahaj mentioned, you will need to create a users node on your firebase database (YourApp/users/userId
) to handle this username. In this node you will store all the user information you want to use in your application.
Doing some research on the internet you can find some examples like this one on how to do it properly.
What you have to do when creating the user is pretty much call a function that will create a new user.
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(LoginActivity.this, "An error occurred", Toast.LENGTH_SHORT).show();
} else {
createNewUser(task.getResult().getUser());
}
hideProgressDialog();
}
)};
private void createNewUser(User userFromRegistration) {
String username = "username";
String email = userFromRegistration.getEmail();
String userId = userFromRegistration.getUid();
User user = new User(username, email);
mDatabase.child("users").child(userId).setValue(user);
}
first of all username is different field which u have to create after login. As u said u know how to register i.e just make their account first were u have its id and password, after that u can create a node in firebaseDatabase like firebse.com/yourApp/users/
were u can save the its other information like username, address, etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With