简体   繁体   English

在删除帐户之前在 Firebase Auth 中验证用户密码

[英]Verify user password in Firebase Auth before deleting account

I use Firebase Auth for user verification and would like to verify a users password before they delete or disable their account.我使用 Firebase Auth 进行用户验证,并希望在用户删除或禁用其帐户之前验证用户密码。 How can this be performed?如何执行此操作?

I have tried the code below, but It does not seem to work.我已经尝试了下面的代码,但它似乎不起作用。

  unregisterUser = (payload) => {
    console.log('unregisterUser payload', payload)
    return firebase.auth().signInWithEmailAndPassword(payload.email, payload.password)
    .then( (user) => {
      //Success
      console.log('signInWithEmailAndPassword ok', user)
      console.log('signIn success')
      return Promise.resolve(true)
    })
    .catch( (error) => {
      //Fail
      console.error('signInWithEmailAndPassword error', error.code, error.message)
      console.error('error', error)
      return Promise.reject(error)
    })
 })

The only console logout I get is我得到的唯一控制台注销是

unregisterUser payload Object { uid: "ABCDEFG12345", password: "111" }

I have read the documentation but cannot seem to find any other way to check a users password.我已阅读文档,但似乎找不到任何其他方法来检查用户密码。

The documentation states that I should first authenticate again and then call firebase.User.reauthenticateWithCredential before firebase.User.delete . 文档指出我应该首先再次进行身份验证,然后在firebase.User.reauthenticateWithCredential之前调用firebase.User.delete

How can this be done?如何才能做到这一点?

Kind regards /K亲切的问候/K

I found out the correct way to do it!我找到了正确的方法!

    const user = firebase.auth().currentUser
    const credential = firebase.auth.EmailAuthProvider.credential(payload.email, payload.password)
    user.reauthenticateWithCredential(credential)
    .then((result) => {
      console.log('result', result)
      // ...
    })
    .catch((error) => {
      console.log('error', error)
      console.log('code', error.code)
      // ...
    })

/K /K

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

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