Reputation: 39
In my app I made an authentification systeme using firebase. I didn't use the default page of firebase but made a custom one with email and password after a click on a button :
mauth.createUserWithEmailAndPassword(lEmail, lPassowrd)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
userManager.createUser(); //CREER USER DANS DATABASE FIRESTORE (PAREIL POUR EMAIL)
Toast.makeText(SignupActivity.this, "Account created !", Toast.LENGTH_SHORT).show();
startActivity(new Intent(SignupActivity.this, LoginActivity.class));
} else{
Toast.makeText(SignupActivity.this, "Registration error: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}});
I added a line to create the user in FireStore, and later in the code I'm using FireStore data to update his username and picture. This leads to a crash as the username is null.
When I check on the Firestore database, a user is created with an id, but the profile picture and username are null. Any idea why? When I just use the information from the FireBase auth the user has an id and a username (no picture but that's ok).
This is the function creating the user on FireStore, previously called :
public void createUser() {
FirebaseUser user = getCurrentUser();
if(user != null){
String urlPicture = (user.getPhotoUrl() != null) ? user.getPhotoUrl().toString() : null;
String username = user.getDisplayName();
//QUAND ON CREER UN DOCUMENT SUR FIRESTORE IL A AUTO UN ID UNIQUE
//MAIS DANS NOTRE CAS L'USER A UN ID LORSQUON L'A CREER AVEC L'AUTHENTIFICATION
String uid = user.getUid();
User userToCreate = new User(uid, username, urlPicture);
Task<DocumentSnapshot> userData = getUserData();
// If the user already exist in Firestore, we get his data (isMentor)
userData.addOnSuccessListener(documentSnapshot -> {
//if (documentSnapshot.contains(IS_MENTOR_FIELD)){
//userToCreate.setIsMentor((Boolean) documentSnapshot.get(IS_MENTOR_FIELD));
//}
this.getUsersCollection().document(uid).set(userToCreate);
});
}
}
A solution would be very appreciated as I'm stuck on this for a while now
EDIT :
I tried modifying the displayName like that :
mauth.createUserWithEmailAndPassword(lEmail, lPassowrd)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
FirebaseUser user = mauth.getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder().setDisplayName(lUsername).build();
//user.updateProfile(profileUpdates);
user.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("filds", "User profile updated.");
}
}
});
userManager.createUser(); //CREER USER DANS DATABASE FIRESTORE (PAREIL POUR EMAIL)
Toast.makeText(SignupActivity.this, "Account created !", Toast.LENGTH_SHORT).show();
startActivity(new Intent(SignupActivity.this, LoginActivity.class));
} else{
Toast.makeText(SignupActivity.this, "Registration error: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}});
The logcat shows "User profile updated." but the username is still null in FireStore :/
Upvotes: 0
Views: 326
Reputation: 139039
When I check on the Firestore database, a user is created with an id, but the profile picture and username are null. Any idea why?
When you authenticate your users using email and password, it means that a new instance of type FirebaseUser is created. Since you are only getting the email address and the password from the user, the only field of the class that is populated is the "email". Since there is no user name involved, nor a profile picture, when you are using getDisplayName() or getPhotoUrl() the result that you get is null, hence that result. If you want to populate those fields too, then you should consider either getting that information from the user, or use of one of the available providers. It can be Google, Facebook, or any other provider. In this way, you'll have both the user name and the profile picture URL populated.
Upvotes: 2