簡體   English   中英

使用 Vue.js 和 Firebase 刪除用戶

[英]Delete user with Vue.js and Firebase

我無法從 vue.js 中的 firebase 中刪除帳戶。 我使用了 firebase 文檔。 這是刪除按鈕:

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

這是方法:

<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>

有人可以幫忙嗎? 帳號還在,無法刪除。 控制台中的零信息。

如果要刪除 Firebase 身份驗證中存在的用戶,您有兩種可能性:

1/ 使用 JavaScript SDK(因為你的應用是用 Vue.js 制作的)

您調用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 {
     //....
  }
})

但是請注意,此方法“要求用戶最近登錄。如果不滿足此要求,請要求用戶再次進行身份驗證,然后調用firebase.User.reauthenticateWithCredential ”。 auth/requires-recent-login代碼的錯誤是“如果用戶的最后一次登錄時間未達到安全閾值,則拋出”。

因此,只有登錄用戶才能從前端調用此方法,以刪除他/她自己的帳戶。

2/ 使用管理員 SDK

您可以使用 Admin SDK 的deleteUser()方法,例如在 Cloud Function 中。

在這種情況下,不需要讓用戶登錄,因為這是在后端執行的,因此可以刪除任何用戶。 例如,您可能有一個由管理員用戶觸發的 Callable Cloud Function。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM