简体   繁体   中英

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()
    }

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() .

try this

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

and

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

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.

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