Firebase 9.0.0 - FirebaseAuthRecentLoginRequiredException和reauthenticate(AuthCredential)

Blo*_*ard 1 android firebase firebase-authentication

我正在使用Firebase 9.0.0.

如果用户想要更改用户电子邮件或密码,Firebase可能会抛出FirebaseAuthRecentLoginRequiredException.

firebaseUser.updateEmail(newEmail).addOnCompleteListener(...
Run Code Online (Sandbox Code Playgroud)

在文档中,有人称在这种情况下称为"reauthenticate(AuthCredential)".在OnCompleteListener中我捕获异常,但我无法创建EmailAuthCredential.该怎么办?我从哪里拿到EmailAuthCredential?

Kat*_*ato 5

它看起来像这样覆盖在这里:

某些安全敏感操作(例如删除帐户,设置主电子邮件地址和更改密码)要求用户最近登录.如果您执行其中一项操作,并且用户已在很久以前登录,则操作失败并抛出FirebaseAuthRecentLoginRequiredException.发生这种情况时,通过从用户获取新的登录凭据并将凭据传递给reauthenticate来重新验证用户身份.例如:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

// Get auth credentials from the user for re-authentication. The example below shows
// email and password credentials but there are multiple possible providers,
// such as GoogleAuthProvider or FacebookAuthProvider.
AuthCredential credential = EmailAuthProvider
        .getCredential("user@example.com", "password1234");

// Prompt the user to re-provide their sign-in credentials
user.reauthenticate(credential)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                Log.d(TAG, "User re-authenticated.");
                // retry changing the password here
            }
        });
Run Code Online (Sandbox Code Playgroud)