使用 Firebase for Android 更改密码 [英] Change password with Firebase for Android

查看:20
本文介绍了使用 Firebase for Android 更改密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to implement change password functionality for my application.

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

I included com.google.firebase:firebase-auth:9.0.2 in my build.gradle file and so far everything has been working fine until I tried to implement change password functionality.

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

I found that the FirebaseUser object has a updatePassword method that takes a new password as the parameter. I could use this method and implement validation myself. However, I need the user's current password for comparing with the inputted one and I can't find a way to get that password.

我还发现了另一个 method 使用旧密码,new密码和处理程序.问题是我还需要包含 com.firebase:firebase-client-android:2.5.2+ 来访问这个类,当我尝试这个方法时,我遇到了以下错误:

I also found another method on the Firebase object that takes the old password, new password, and a handler. The problem is that I need to also include com.firebase:firebase-client-android:2.5.2+ to access this class and when I am trying this method I'm getting to following error:

在 console.firebase.google.com 上创建的项目必须使用 firebase.google.com/docs/auth/提供的新 Firebase 身份验证 SDK

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

感觉我在这里遗漏了一些东西.建议的实施方法是什么?什么时候使用什么依赖?

Feel like I'm missing something here. What's the recommended approach for implementing this? And when to use what dependency?

推荐答案

我在 Firebase 文档:

一些对安全敏感的操作——例如删除帐户、设置主电子邮件地址和更改密码——要求用户最近登录.如果您执行这些操作之一,并且用户很久以前登录,操作失败并抛出FirebaseAuthRecentLoginRequiredException.当这个情况发生时,通过从新的登录凭据重新验证用户用户并传递凭据以重新进行身份验证.例如:

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

这篇关于使用 Firebase for Android 更改密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆