我正在将一个 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/