简体   繁体   中英

Delete user with Vue.js and Firebase

I cant remove account from firebase in vue.js. I used firebase docs. 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:

1/ Using the JavaScript SDK (since your app is made with Vue.js)

You call the delete() method, as follows:

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

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

You can use the Admin SDK's deleteUser() method, for example within a 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.

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