简体   繁体   English

如何使用 Vue.js Firebase UID 删除用户

[英]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.我试图在我的 Vue.js Firebase 应用程序中实现一个通过 UID 删除/移除用户的功能。 The app is set up to enable registration of users in Firebase authentication via email and password.该应用程序设置为允许通过电子邮件和密码在 Firebase 身份验证中注册用户。 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.登录后,用户应该能够单击删除以从 Firebase 身份验证中删除他/她的电子邮件和密码以及用户数据。 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. ...但我发现 user.delete() 不是一个函数。 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.在您的代码中, user是一个DocumentSnapshot类型的对象。 If you want to delete the document, you can use user.ref.delete() .如果要删除文档,可以使用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.删除文档不会同时删除 Firebase 身份验证中的用户帐户。 If you want to delete the user account, you will have to use the Firebase Authentication API for that.如果要删除用户帐户,则必须为此使用 Firebase Authentication API。 firebase.auth().currentUser.delete() . 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. await db.collection("users").doc(user).delete()从 DOM 中的 click 事件中接受“user”作为参数,以便定位数据库中指定用户的文档(我不不知道为什么我没有早点想到这一点!) await firebase.auth().currentUser.delete()从 firebase 授权中删除 currentUser。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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