📌  相关文章
📜  如何从Android App删除Firebase用户?

📅  最后修改于: 2021-05-13 17:58:49             🧑  作者: Mango

我们已经在许多应用程序中看到,如果可以选择删除用户或可以永久地说出您的帐户,则可以选择。在这里,我们将看到如何在应用程序中实现该功能,以及如何从Firebase身份验证中永久删除我们的帐户。在这里,在编写代码时,我们将删除用户,但是在此之前,我们将重新验证用户身份。

分步实施

第1步:如果您不熟悉Firebase,则可以参考Android中使用Firebase的用户身份验证。

第2步:按照所有步骤操作,然后“登录”到您的android应用。然后创建一个mainmenu.xml文件,如下所示

XML


  
    
    
    
      


Java
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.tabs.TabLayout;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
  
public class MainActivity extends AppCompatActivity {
    
    private FirebaseAuth mAuth;
  
    private ViewPager mviewPager;
    private FirebaseAuth firebaseAuth;
    private Toolbar mtoolbar;
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private TabLayout mTabLayout;
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAuth = FirebaseAuth.getInstance();
        mtoolbar = (Toolbar)findViewById(R.id.main_page_toolbar);
    }
  
    @Override public boolean onCreateOptionsMenu(Menu menu)
    {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.mainmenu, menu);
        return true;
    }
  
    @Override
    public boolean
    onOptionsItemSelected(@NonNull MenuItem item)
    {
        super.onOptionsItemSelected(item);
        if (item.getItemId() == R.id.menulogout) {
            FirebaseAuth.getInstance().signOut();
            senttostart();
        }
        if (item.getItemId() == R.id.delete) {
            startActivity(new Intent(MainActivity.this, DeleteUser.class));
        }
        return true;
    }
}


XML


  
    
  
    
  
    


Java
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
  
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
  
public class DeleteUser extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_user);
        final EditText email = findViewById(R.id.email);
        final EditText password = findViewById(R.id.logpass);
        Button submit = findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteuser(email.getText().toString(), password.getText().toString());
            }
        });
    }
  
    private void deleteuser(String email, String password) {
          
        final 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(email, password);
  
        // Prompt the user to re-provide their sign-in credentials
        if (user != null) {
            user.reauthenticate(credential)
                    .addOnCompleteListener(new OnCompleteListener() {
                        @Override
                        public void onComplete(@NonNull Task task) {
                            user.delete()
                                    .addOnCompleteListener(new OnCompleteListener() {
                                        @Override
                                        public void onComplete(@NonNull Task task) {
                                            if (task.isSuccessful()) {
                                                Log.d("TAG", "User account deleted.");
                                                startActivity(new Intent(DeleteUser.this, StartActivity.class));
                                                Toast.makeText(DeleteUser.this, "Deleted User Successfully,", Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    });
                        }
                    });
        }
    }
}


步骤3:创建一个新的空活动并将该活动命名为DeleteUser

步骤4:然后转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。

Java

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.tabs.TabLayout;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
  
public class MainActivity extends AppCompatActivity {
    
    private FirebaseAuth mAuth;
  
    private ViewPager mviewPager;
    private FirebaseAuth firebaseAuth;
    private Toolbar mtoolbar;
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private TabLayout mTabLayout;
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAuth = FirebaseAuth.getInstance();
        mtoolbar = (Toolbar)findViewById(R.id.main_page_toolbar);
    }
  
    @Override public boolean onCreateOptionsMenu(Menu menu)
    {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.mainmenu, menu);
        return true;
    }
  
    @Override
    public boolean
    onOptionsItemSelected(@NonNull MenuItem item)
    {
        super.onOptionsItemSelected(item);
        if (item.getItemId() == R.id.menulogout) {
            FirebaseAuth.getInstance().signOut();
            senttostart();
        }
        if (item.getItemId() == R.id.delete) {
            startActivity(new Intent(MainActivity.this, DeleteUser.class));
        }
        return true;
    }
}

步骤5:使用activity_deleteuser.xml文件

导航至应用程序> res>布局> activity_deleteuser.xml,然后将以下代码添加到该文件中。以下是activity_deleteuser.xml文件的代码我们将创建两个简单的EditText和一个Button

XML格式



  
    
  
    
  
    

步骤4:使用DeleteUser。 Java文件

转到DeleteUser。 Java文件并参考以下代码。下面是DeleteUser的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
  
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
  
public class DeleteUser extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_user);
        final EditText email = findViewById(R.id.email);
        final EditText password = findViewById(R.id.logpass);
        Button submit = findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteuser(email.getText().toString(), password.getText().toString());
            }
        });
    }
  
    private void deleteuser(String email, String password) {
          
        final 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(email, password);
  
        // Prompt the user to re-provide their sign-in credentials
        if (user != null) {
            user.reauthenticate(credential)
                    .addOnCompleteListener(new OnCompleteListener() {
                        @Override
                        public void onComplete(@NonNull Task task) {
                            user.delete()
                                    .addOnCompleteListener(new OnCompleteListener() {
                                        @Override
                                        public void onComplete(@NonNull Task task) {
                                            if (task.isSuccessful()) {
                                                Log.d("TAG", "User account deleted.");
                                                startActivity(new Intent(DeleteUser.this, StartActivity.class));
                                                Toast.makeText(DeleteUser.this, "Deleted User Successfully,", Toast.LENGTH_LONG).show();
                                            }
                                        }
                                    });
                        }
                    });
        }
    }
}

输出: