2016-06-14 144 views
3

我正在將Android應用程序從舊版本遷移到新的Firebase平臺。如果給定未知的電子郵件字符串,我似乎無法使sendPasswordResetEmail按照電子郵件/密碼身份驗證的文檔工作。Firebase sendPasswordResetEmail似乎無法與firebase-auth正常工作:9.0.2

文檔說:

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

這裏是我的PW復位方法:

// 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()); 
    } 
} 

調用此方法導致此編譯時錯誤(該IDE也標誌):

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

如果我省略了try-catch代碼,那麼該方法會編譯,但返回的任務永遠不會成功,即使已知od電子郵件地址。

好消息是,Firebase最終會將重置設置發送到正確的地址,但我想知道爲什麼sendPasswordResetEmail在給出未知的用戶電子郵件或給定有效的電子郵件時不會拋出記錄的異常。

我在5月18日的發行說明中看到,此功能存在iOS問題。

回答

8

FirebaseAuth.sendPasswordResetEmail(...)返回Task

A Task表示最終結果異步完成。這也是爲什麼task.isSuccessful()將返回false,當您檢查請求後是否立即完成。

你應該做的是:

 
mAuth.sendPasswordResetEmail(email) 
    .addOnSuccessListener(new OnSuccessListener() { 
     public void onSuccess(Void result) { 
     // send email succeeded 
     } 
    }).addOnFailureListener(new OnFailureListener() { 
     public onFailure(Exception e) 
     // something bad happened 
     } 
    }); 
0

簡單的方法來做到這一點只需使用此功能

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(); 
         } 
        } 
       }); 
    }