简体   繁体   English

使用 Vue.js 和 Firebase 删除用户

[英]Delete user with Vue.js and Firebase

I cant remove account from firebase in vue.js.我无法从 vue.js 中的 firebase 中删除帐户。 I used firebase docs.我使用了 firebase 文档。 Here is button to delete:这是删除按钮:

<template>
[...]
      <div class="text-center">
        <button type="button" class="btn text-white my-4" @click="$emit('deleteUser')">Delete account</button>
      </div>
[...]
</template>

Here is method:这是方法:

<script>
[...]
import firebase from "firebase"
import {router} from '../main'

export default {
    [...]
  },
  methods: {
    [...]
  deleteUser () {
    //const userRef = firebase.auth().currentUser;

    this.usersRef.remove().then(function() {
      // User deleted.
      console.log("User deleted")
      router.push('/')
    }).catch(err => {
          this.error = err.message
      // An error happened.
      console.log("User NOT deleted")
    });
  }
};
</script>

Someone can help?有人可以帮忙吗? Account is still, and cant remove.帐号还在,无法删除。 Zero info in console.控制台中的零信息。

If you want to delete a user existing in Firebase authentication you have two possibilities:如果要删除 Firebase 身份验证中存在的用户,您有两种可能性:

1/ Using the JavaScript SDK (since your app is made with Vue.js) 1/ 使用 JavaScript SDK(因为你的应用是用 Vue.js 制作的)

You call the delete() method, as follows:您调用delete()方法,如下所示:

const user = firebase.auth().currentUser;
user.delete()
.then(() => {
   //....
})
.catch(err => {
  if (err.code === "auth/requires-recent-login") {
     //Re-authenticate the user and call again the Vue.js method
  } else {
     //....
  }
})

Note however, that this method "requires the user to have recently signed in. If this requirement isn't met, ask the user to authenticate again and then call firebase.User.reauthenticateWithCredential ".但是请注意,此方法“要求用户最近登录。如果不满足此要求,请要求用户再次进行身份验证,然后调用firebase.User.reauthenticateWithCredential ”。 An error with the auth/requires-recent-login code is "thrown if the user's last sign-in time does not meet the security threshold". auth/requires-recent-login代码的错误是“如果用户的最后一次登录时间未达到安全阈值,则抛出”。

So, only the logged-in user can call this method from a front-end , in order to delete his/her own account.因此,只有登录用户才能从前端调用此方法,以删除他/她自己的帐户。

2/ Using the Admin SDK 2/ 使用管理员 SDK

You can use the Admin SDK's deleteUser() method, for example within a Cloud Function.您可以使用 Admin SDK 的deleteUser()方法,例如在 Cloud Function 中。

In this case, there is no need to have the user logged-in since this is executed in the back-end and it is therefore possible to delete any user.在这种情况下,不需要让用户登录,因为这是在后端执行的,因此可以删除任何用户。 For example, you could have a Callable Cloud Function triggered by an admin user.例如,您可能有一个由管理员用户触发的 Callable Cloud Function。

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

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