android - 使用 Firebase for Android 更改密码

标签 android firebase firebase-authentication

我想为我的应用程序实现更改密码功能。

我将 com.google.firebase:firebase-auth:9.0.2 包含在我的 build.gradle 文件中,到目前为止一切正常,直到我尝试实现修改密码功能。

我发现 FirebaseUser 对象有一个 updatePassword 方法,该方法将新密码作为参数。我可以使用这种方法并自己实现验证。但是,我需要用户的当前密码与输入的密码进行比较,但我找不到获取该密码的方法。

我还找到了另一个 method在接受旧密码、新密码和处理程序的 Firebase 对象上。问题是我还需要包含 com.firebase:firebase-client-android:2.5.2+ 来访问这个类,当我尝试这种方法时,我遇到了以下错误:

Projects created at console.firebase.google.com must use the new Firebase Authentication SDKs available from firebase.google.com/docs/auth/

感觉好像我在这里遗漏了一些东西。实现此操作的推荐方法是什么?以及什么时候使用什么依赖?

最佳答案

我在 Firebase docs 中找到了一个方便的示例。 :

Some security-sensitive actions—such as deleting an account, setting a primary email address, and changing a password—require that the user has recently signed in. If you perform one of these actions, and the user signed in too long ago, the action fails and throws FirebaseAuthRecentLoginRequiredException. When this happens, re-authenticate the user by getting new sign-in credentials from the user and passing the credentials to reauthenticate. For example:

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) {
                if (task.isSuccessful()) {
                    user.updatePassword(newPass).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                Log.d(TAG, "Password updated");
                            } else {
                                Log.d(TAG, "Error password not updated")
                            }
                        }
                    });
                } else {
                    Log.d(TAG, "Error auth failed")
                }
            }
        });

关于android - 使用 Firebase for Android 更改密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39866086/

相关文章:

javascript - react native Firebase图像上传错误404

swift - 当前用户 UID 与身份验证 UID 不同吗?

firebase - 在 Tapkey 上处理过期 Firebase token 的最佳方法

android - 如何退出谷歌认证?

android - Firebase firestore 变量名称已更改

javascript - 为什么我的 Google 身份验证在 Firebase 中不起作用?

iphone - 读取大量数据时哪个更快 : XML or SQLite

android - 将 Fragment 中的 Parcelable 对象共享到 Activity

android - 在 TextView 中垂直对齐由 HTML 定义的图像

android - "Profile or Debug APK..."选项在 Android Studio 3.4.1 中消失到哪里去了?