I am working on an android application that involves user firebase auth for user sign up. The sign up works but I want to add username to database by implementing it in .then() but my android studio keeps giving "unresolved method then(?)". I am also using catch but that seems to work fine.The code I am writing is as following where firebaseAuth is an object of type FirebaseAuth:
firebaseAuth.createUserWithEmailAndPassword(email,password)
.then( (u) => {
//Implementation of then
})
.catch(error => {
switch (error.code) {
case 'auth/email-already-in-use':
EmailWarning.setText("Email already in use");
case 'auth/invalid-email':
EmailWarning.setText("Invalid Email");
case 'auth/weak-password':
PasswordWarning.setText("Password should be 8 characters or longer");
default:
PasswordWarning.setText("Error during sign up");
}
});
I found a similar problem in following link but even after trying this, it's not working.
I looked further into the firebase documentation and found another implementation which uses on complete listener here however the error codes described in this documentation doesn't seem to work with it.
Update 1: I ended up implementing on complete listener as following:
firebaseAuth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = firebaseAuth.getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(Username)
.build();
user.updateProfile(profileUpdates);
}
else {
// If sign up fails, display a message to the user.
switch (task.getException()) {
case "auth/email-already-in-use":
EmailWarning.setText("Email already in use");
case "auth/invalid-email":
EmailWarning.setText("Invalid Email");
case "auth/weak-password":
PasswordWarning.setText("Password should be 8 characters or longer");
default:
PasswordWarning.setText("Error during sign up");
}
}
}
});
Now the user adding part works fine but the exception handler doesn't work with strings so I can't find a way to work with error codes given on firebase documentation
task.getError(), I dont thinktask.getException()exists -> firebase.google.com/docs/reference/android/io/fabric/sdk/…isFinished()instead ofisSuccessful()-isFinished()should only be called if it finishes without error, from same doc link above.task.getError()to make sure you know what the strings are, you are probably only getting into the error block becauseisSuccessful()isn't a function.task.getError()will return aThrowable-> firebase.google.com/docs/reference/android/io/fabric/sdk/… - it looks like you can usegetMessage()ortoString()to get string representations of the error. To get the actualThrowableyou can usegetCause()- this would return an exception object likeInitializationException, and if you know which exceptions are possible you can use==with those.