android - Firebase sendPasswordResetEmail 似乎无法与 firebase-auth :9. 0.2 一起正常工作

标签 android firebase-authentication

我正在将一个 Android 应用程序从旧平台迁移到新的 Firebase 平台。如果给定一个未知的电子邮件字符串,我似乎无法让 sendPasswordResetEmail 按照电子邮件/密码身份验证的文档工作。

文档说:

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

这是我的密码重置方法:

 // 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 代码,那么该方法会编译,但返回的任务永远不会成功,即使使用已知的良好电子邮件地址也是如此。

好消息是 Firebase 最终会向正确的地址发送重置,但我想知道为什么 sendPasswordResetEmail 在给定未知用户电子邮件或在给定有效电子邮件时成功执行任务时不抛出记录的异常。

我确实在 5 月 18 日的发行说明中看到此功能存在 iOS 问题。

最佳答案

FirebaseAuth.sendPasswordResetEmail(...) 返回 Task .

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

关于android - Firebase sendPasswordResetEmail 似乎无法与 firebase-auth :9. 0.2 一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37822447/

相关文章:

swift - 如何知道 fetchProviders() 何时完成执行?

android - Firebase:发生内部错误 [QUOTA_EXCEEDED]

javascript - Firebasefunctions.auth.user().onCreate不触发

javascript - 火力地堡 4.1.3 : onAuthStateChange not called after first sign in with email and password

javascript - Firebase 谷歌身份验证问题

Android CheckBoxPreference 默认值

android - 我应该如何通过 Activity 链转发 Intent 参数?

android - 无法通过 `api` 方法从 Android 模块解析主题和样式

android - Webview加载远程url

java - 从 onpostexecute 发送值