从“管理员用户"重新认证用户x.删除用户x [英] Reauthenticate user x from an "admin user" to delete user x

查看:35
本文介绍了从“管理员用户"重新认证用户x.删除用户x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其他带有removeUser的Firebase版本中,我们只能使用电子邮件和密码来删除用户.使用新的Firebase版本,似乎只有与该用户建立连接的用户才能删除和....但是,当我与admin用户建立连接并尝试删除另一个用户时,问题就来了.这是代码.

In others Firebase version with removeUser we can delete an user only using email and password. With the new Firebase version it seems you can only delete and user if you have connected with that one... But the problem comes when I am connected with the admin user and I tried to delete the other user. This is the code.

    final FirebaseUser user = mAuth.getCurrentUser();

 AuthCredential credential = EmailAuthProvider
  .getCredential(mail, postSnapshot.getValue(User.class).getPwdUser());

 user.reauthenticate(credential)
  .addOnCompleteListener(new OnCompleteListener < Void > () {
   @Override
   public void onComplete(@NonNull Task < Void > task) {

    user.delete().addOnCompleteListener(new OnCompleteListener < Void > () {
     @Override
     public void onComplete(@NonNull Task < Void > taskDeleted) {
      if (taskDeleted.isSuccessful()) {
       Toast.makeText(getApplicationContext(),
        "Deleted user!", Toast.LENGTH_LONG).show();
      }
     }
    });
   }
  });

但是我无法获取delete方法,因为在重新验证时它会引发错误,提示提供的凭据与先前登录的用户不符."有人知道我可以如何从另一个用户重新进行身份验证吗?

But I can't get delete method because on reauthenticate it throws an error with "The supplied credentials do not correspond to the previously signed in user." Anyone knows how I can reauthenticate from another user?

推荐答案

最后,代码应该是这样的

Finally the code should be something like this

 mAuth.signOut();
mAuth.signInWithEmailAndPassword(email,password)
        .addOnCompleteListener(UserList.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (!task.isSuccessful()) {
                    mAuth= FirebaseAuth.getInstance(myFirebaseRef.getDatabase().getApp());
                    try{

                        mAuth.signInWithEmailAndPassword(getsPreferences().getString("mailUser",""), getsPreferences().getString("pwd",""))
                                .addOnCompleteListener(UserList.this, new OnCompleteListener<AuthResult>() {
                                    @Override
                                    public void onComplete(@NonNull Task<AuthResult> task) {

                                    }});
                    }catch(Exception e){

                    }
                }
            }});

这篇关于从“管理员用户"重新认证用户x.删除用户x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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