JS_is_awesome18
JS_is_awesome18

Reputation: 1757

How to delete user with Vue.js Firebase UID

I am attempting to implement a function in my Vue.js Firebase app that deletes/removes users by UID. The app is set up to enable registration of users in Firebase authentication via email and password. Once logged in, the user should be able to click delete in order to remove his/her email and password from Firebase authentication, as well as user data. So far I have the following for the delete function:

    async deleteProfile () {
      let ref = db.collection('users')
      let user = await ref.where('user_id', '==',firebase.auth().currentUser.uid).get()
      user.delete()
    }

...but I am getting user.delete() is not a function. How can I set up this function to delete the user in authentication and database? Thanks!

UPDATED FUNCTION

    async deleteProfile () {
      let ref = db.collection('users')
      let user = await ref.where('user_id', '==', firebase.auth().currentUser.uid).get()
      await user.ref.delete()
      await firebase.auth().currentUser.delete()
    }

Upvotes: 0

Views: 1132

Answers (3)

Ham-
Ham-

Reputation: 11

try this

<button class="..." @click="deleteProfile(currentUser.uid)">Delete</button>

and

methods: {
async deleteProfile(dataId) {
      fireDb.collection("yourCollection").doc(dataId).delete()
      .then(() => {
        alert('Deleted')
      })
    }
}

Upvotes: 1

JS_is_awesome18
JS_is_awesome18

Reputation: 1757

Building off Doug Stevenson's answer, this is the function that ultimately worked.

    async deleteProfile (user) {
      await db.collection("users").doc(user).delete()
      await firebase.auth().currentUser.delete()
    }

await db.collection("users").doc(user).delete() accepts "user" as an argument from the click event in the DOM, in order to target the doc of the specified user in the database (I don't know why I didn't think of that sooner!) await firebase.auth().currentUser.delete() removes currentUser from firebase authorization.

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317372

In your code, user is a DocumentSnapshot type object. If you want to delete the document, you can use user.ref.delete(). It returns a promise, so you will need to await it.

Deleting a document will not also delete a user account in Firebase Authentication. If you want to delete the user account, you will have to use the Firebase Authentication API for that. firebase.auth().currentUser.delete().

Upvotes: 3

Related Questions