简体   繁体   中英

Getting "TypeError: user.getIdToken is not a function" when updating password in firebase

When I run the function updatePassword I get the error "TypeError: user.getIdToken is not a function", which comes from a file in the firebase sdk.

async changePassword(state, payload) {
      const user = auth.currentUser
      let cred = EmailAuthProvider.credential(
        payload.email,
        payload.oldPassword
      )

      reauthenticateWithCredential(user, cred)
        .then(() => {
          // User re-authenticated.
          console.log(payload.newPassword)
          updatePassword(payload.email, payload.newPassword)
            .then(() => {
              // Update successful.
              console.log('Succeed')
            })
            .catch((error) => {
              console.log('Failed', error)
              // An error ocurred
              // ...
            })
        })
        .catch((error) => {
          // An error ocurred
          // ...
          console.log(error)
        })
    },

The updatePassword() function takes User as first parameter and not user's email. Try refactoring the code as shown below:

reauthenticateWithCredential(user, cred).then(({ user }) => {
  // Pass user here and not payload.email 
  updatePassword(user, payload.newPassword).then(() => {
    console.log('Succeed')
  }) 
})

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