简体   繁体   中英

Is there a way to delete anonymous user from Firebase Database Authentication and Firebase Database UID?

What currently happens: when user logs out as an anonymous user using onBackPressed and case R.id.logoutMenuOption , their Firebase Auth and Firebase Database Uid gets removed correctly. Except, when a user swipe closes the app the onDestroy() method is called but their Firebase Auth and Firebase Database Uid still remains in the Database. I use the onDestroy() when the user swipe closes the app.

What I need to happen: when a user swipe closes the app, the onDestroy() and removeAnonymousVendorUser() methods are called to delete the anonymous users authentication as well as their User Uid from the Realtime Database.

What I have done so far: I have created the removeAnonymousVendorUser() method and placed it in the onDestroy() method. I also will show how it is used in my other logout methods, and in those methods it does work, it just does not work for the onDestroy()

  @Override
protected void onDestroy() {
    super.onDestroy();
    //TODO: Figure out how to delete UID and user when they sign out
    removeAnonymousVendorUser();
    FirebaseAuth.getInstance().signOut();
    finish();
}

Method that removes the anonymous user Firebase Auth data and Firebase Database data:

  private void removeAnonymousVendorUser() {

    if (FirebaseAuth.getInstance().getCurrentUser() != null) {

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

        user.delete();
    }

    DatabaseReference vendorIdReference = FirebaseDatabase.getInstance().getReference("Vendor");
    vendorIdReference.removeValue();

}

Logout Method when user clicks Logout menu option button:

           //Logout Menu Option Button
              @Override
            public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {

             // If Logout Button is clicked within 2 Seconds log user out
            case R.id.logoutMenuOption:
            if (logoutAppPressAgain + 2000 > System.currentTimeMillis()) {
                logoutToast.cancel();

                removeAnonymousVendorUser();
                FirebaseAuth.getInstance().signOut();
                removeAnonymousVendorUser();
                Intent logoutIntent = new Intent(VendorMapsActivity.this, WelcomeActivity.class);
                startActivity(logoutIntent);
                finish();
            } else {
                logoutToast = Toast.makeText(getBaseContext(), R.string.press_button_again_to_logout, Toast.LENGTH_SHORT);
                logoutToast.show();
            }

            logoutAppPressAgain = System.currentTimeMillis();
    }

    return super.onOptionsItemSelected(item);
     }

Log out method when user presses back twice:

        public void onBackPressed() {
    if (exitAppPressBack + 2000 > System.currentTimeMillis()) {
        exitAppBackToast.cancel();
        super.onBackPressed();

        removeAnonymousVendorUser();
        FirebaseAuth.getInstance().signOut();
        Intent logoutIntent = new Intent(VendorMapsActivity.this, 
        WelcomeActivity.class);
        startActivity(logoutIntent);
        finish();
        } else {
        exitAppBackToast = Toast.makeText(getBaseContext(), 
        R.string.press_back_one_more_time_to_exit, Toast.LENGTH_SHORT);
        exitAppBackToast.show();
        }

    exitAppPressBack = System.currentTimeMillis();
}

This is the code that creates the users when they login anonymously:

     public void sellAnonymously(View view) {

    circProgressBar.setVisibility(View.VISIBLE);

    firebaseAuth.signInAnonymously().addOnSuccessListener(VendorLoginActivity.this, new OnSuccessListener<AuthResult>() {
        @Override
        public void onSuccess(AuthResult authResult) {

            Toast.makeText(VendorLoginActivity.this, "Success!  Enjoy the app!", Toast.LENGTH_LONG).show();
            String client_id = firebaseAuth.getCurrentUser().getUid();
            DatabaseReference client_db = FirebaseDatabase.getInstance().getReference().child("Vendor").child(client_id);
            client_db.setValue(true);
            circProgressBar.setVisibility(View.INVISIBLE);

            // Sends anonymous user to VendorMapsActivity
            startActivity(new Intent(VendorLoginActivity.this, VendorMapsActivity.class));

        }
    });
}

To delete a user, call user.delete() as shown in the Firebase documentation on deleting a user :

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

user.delete()

To delete data from the database for the current user, call removeValue() on a reference to that data as shown in the Firebase documentation on deleting data :

ref.removeValue()

You can combine the two by using Cloud Functions for Firebase. In that case you respond to the user being deleted, by deleting their data from the database. For a short explanation of this, see the Firebase documentation on triggering Cloud Functions when a user is deleted :

exports.deleteUserData = functions.auth.user().onDelete((user) => {
  // ...
})
User user = FirebaseAuth.instance.currentUser;
await user.delete();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM