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

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

问题描述

我想为我的应用程序实现更改密码功能。
$ b 我包含 com.google.firebase:firebase-auth:9.0.2 在我的 build.gradle 文件中,到目前为止,一切工作都正常,直到我试图实现更改密码功能。



我发现 FirebaseUser 对象有一个 updatePassword 方法,它将一个新的密码作为参数。我可以使用这个方法并自己实现验证。然而,我需要用户的当前密码与输入的比较,我无法找到一个方法来获得密码。



我还发现另一个对象上的.firebase.client.Firebase.ResultHandler- =noreferrer>方法,该对象将旧密码,新密码和处理程序。问题是我还需要包含 com.firebase:firebase-client-android:2.5.2 + 来访问这个类,当我尝试这个方法时,我出现以下错误:


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


感觉我在这里错过了一些东西。推荐的方法是什么?什么时候使用什么依赖?

解决方案

我在 Firebase文档: p>


一些安全敏感的操作(例如删除帐户,设置
主电子邮件地址以及更改密码)要求用户
最近已经登录。如果您执行了其中一个操作,并且
用户过早登录,则操作将失败并引发
FirebaseAuthRecentLoginRequiredException。发生这种情况时,
通过从
用户获取新的登录凭据并传递凭据进行重新验证来重新对用户进行身份验证。例如:



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

//从用户获取授权凭证以进行重新验证。以下示例显示了
//电子邮件和密码凭据,但有多个可能的提供程序,
//例如GoogleAuthProvider或FacebookAuthProvider。
AuthCredential凭证= EmailAuthProvider
.getCredential(user@example.com,password1234);

//提示用户重新提供登录凭据
user.reauthenticate(凭证)
.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, 密码更新);
} else {
Log.d(TAG,错误密码未更新)
}
}
});
} else {
Log.d(TAG,错误验证失败 )
}
}
});


I want to implement change password functionality for my application.

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.

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.

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:

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?

解决方案

I found a handy example of this in the 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")
                }
            }
        });

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

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