'Firebase Delete User Authentication on Android
Is there a way to delete a user from firebase authentication, when the current user is not the one who is being deleted.
I know that there is a way to delete a current user by FirebaseAuth.getInstance().getCurrentUser().delete()
, but the issue is that the current user is an admin who is trying to delete some other user account so cant use the above code.
Solution 1:[1]
The client-side Firebase SDKs have no concept of an admin user, and only allow deleting the currently signed in user.
If you want to delete any other user, you can do so with the Admin SDKs, which are designed to be used in a trusted environment, such as your development machine, a server you control, or Cloud Functions.
To expose such functionality to your application administrator in the app itself, you can wrap the functionality in a custom API endpoint and then call that from the client-side application code. Just make sure to ensure the user is authorized for the operation.
Solution 2:[2]
you cannot delete users from Firebase Client SDK. You need to use the Firebase Admin SDK to delete any user.
I'm not completely sure how you are distinguishing between normal users and admins, but the best way is to use Custom Claims. They are like roles you assign to users.
The Admin SDK gives you full access to all the Firebase resources, that means you can add, update, delete anything in Firebase whether it be firestore, storage or authentication. That being said, you must keep the Service Account Key required to use Admin SDK a secret. Therefore you must use this in secure environment like your server or cloud functions only. You can try the following code to delete users.
// First verify if user is admin
// if yes then proceed.
admin
.auth()
.deleteUser(uid)
.then(() => {
console.log('Successfully deleted user');
})
.catch((error) => {
console.log('Error deleting user:', error);
});
Let me know if you have any subsequent questions.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Frank van Puffelen |
Solution 2 | Dharmaraj |