Reputation: 11
This is all the code with register account with email password, save verify email, save user data into Firestore database. Only the Firestore database can't run.
fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
// send verification link
FirebaseUser fuser = fAuth.getCurrentUser();
fuser.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(register.this, "Verification Email Has been Sent.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: Email not sent " + e.getMessage());
}
});
Toast.makeText(register.this, "User Created.", Toast.LENGTH_SHORT).show();
userID = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fStore.collection("users").document(userID);
Map<String,Object> user = new HashMap<>();
user.put("fName",fullName);
user.put("email",email);
user.put("phone",phone);
documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: user Profile is created for "+ userID);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "onFailure: " + e.toString());
}
});
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
else { Toast.makeText(register.this, "Error ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show(); progressBar.setVisibility(View.GONE); } } }); } });
Upvotes: 1
Views: 257
Reputation: 138814
The Firebase authentication operation is asynchronous. This means that you can be sure that authentication succeeded only when the onSuccess()
method fires. Since you say that the authentication process successfully completes, your first onSuccess()
is triggered. Your second onSuccess()
is not triggered because the code that corresponds to the addition of data to Firestore is outside the callback, hence that behavior. To solve this, all the code that exists outside the callback should be moved inside the first onSuccess()
as explained in the following line of code:
fAuth
.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
// send verification link
FirebaseUser fuser = fAuth.getCurrentUser();
fuser.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(register.this, "Verification Email Has been Sent.", Toast.LENGTH_SHORT).show();
Toast.makeText(register.this, "User Created.", Toast.LENGTH_SHORT).show();
//Moved code 👈👈👈
userID = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fStore.collection("users").document(userID);
Map<String,Object> user = new HashMap<>();
user.put("fName",fullName);
user.put("email",email);
user.put("phone",phone);
documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: user Profile is created for "+ userID);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "onFailure: " + e.toString());
}
});
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: Email not sent " + e.getMessage());
}
});
} else {
Toast.makeText(register.this, "Error ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
In this way, you be able to write the data to Firestore when the authentication process is completed.
Upvotes: 4