Firebase sendPasswordResetEmail 似乎无法与 firebase-auth:9.0.2 一起正常工作
Firebase sendPasswordResetEmail doesn't seem to work correctly with firebase-auth:9.0.2
我正在将 Android 应用程序从旧平台迁移到新的 Firebase 平台。如果给定一个未知的电子邮件字符串,我似乎无法让 sendPasswordResetEmail 像 email/password 身份验证记录的那样工作。
文档说:
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(...)
return一个Task。
A Task
表示异步完成的最终结果。这也是为什么 task.isSuccessful()
会 return 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
}
});
简单的方法只需使用这个函数
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();
}
}
});
}
我正在将 Android 应用程序从旧平台迁移到新的 Firebase 平台。如果给定一个未知的电子邮件字符串,我似乎无法让 sendPasswordResetEmail 像 email/password 身份验证记录的那样工作。
文档说:
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(...)
return一个Task。
A Task
表示异步完成的最终结果。这也是为什么 task.isSuccessful()
会 return 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 } });
简单的方法只需使用这个函数
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();
}
}
});
}