Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase sendPasswordResetEmail doesn't seem to work correctly with firebase-auth:9.0.2

I am moving an Android app to the new Firebase platform from the old one. I can't seem to get sendPasswordResetEmail to work as documented for email/password authentication if given an unknown email string.

The documentation says:

public Task sendPasswordResetEmail (String email)

Triggers the Firebase Authentication backend to send a password-reset email to the given email address, which must correspond to an existing user of your app.

Exceptions:

FirebaseAuthInvalidUserException thrown if there is no user corresponding to the given email address Returns Task to track completion of the sending operation

Here is my pw reset method:

 // firebase password reset
private void requestPwReset() {
    String email = mEmailView.getText().toString();
    Log.d(TAG, "sending pw reset request for: " + email);
    try {
        Task<Void> task = mAuth.sendPasswordResetEmail(email);
        Log.d("TAG", "result: " + (task.isSuccessful() == true) ); // NEVER SUCCEEDS, EVEN WITH VALID EMAIL ADDRESS
    } catch(FirebaseAuthInvalidUserException e) {  //COMPILE ERROR HERE!
        Log.d(TAG, "exception: " + e.toString());
    }
}

Calling this method causes this compile time error (which the IDE also flags):

LoginActivity.java:117: error: exception FirebaseAuthInvalidUserException is never thrown in body of corresponding try statement } catch(FirebaseAuthInvalidUserException e) {

If I omit the try-catch code then the method compiles, but the returned task never succeeds, even with known good email addresses.

The good news is that Firebase does eventually send a reset to good addresses, but I'm wondering why the sendPasswordResetEmail doesn't throw the documented exception if given an unknown user email or a successful Task when given a valid email.

I did see in the May 18 release notes that there is an iOS issue with this function.

like image 295
Jim In Texas Avatar asked Jun 14 '16 21:06

Jim In Texas


2 Answers

FirebaseAuth.sendPasswordResetEmail(...) returns a Task.

A Task represents an eventual result which completes asynchronously. This is also why task.isSuccessful() will return false when you check if it has completed immediately after making the request.

What you should be doing is:

mAuth.sendPasswordResetEmail(email)
  .addOnSuccessListener(new OnSuccessListener() {
      public void onSuccess(Void result) {
        // send email succeeded
      }
   }).addOnFailureListener(new OnFailureListener() {
      public onFailure(Exception e)
        // something bad happened
      }
   });
like image 93
Sparq Avatar answered Oct 12 '22 10:10

Sparq


Easy way to do this Simply use this function

    private void resetPassword(final String email) {
        mAuth.sendPasswordResetEmail(email)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(mActivity, "Reset email instructions sent to " + email, Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(mActivity, email + " does not exist", Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }
like image 29
Sohaib Aslam Avatar answered Oct 12 '22 11:10

Sohaib Aslam