Search code examples
javascriptfirebaseweb-applicationsfirebase-authentication

How do I modify my delete account function to work for Google accounts?


My web app uses Firebase auth for email and password, and signing in with Google. I have this function that is intended to delete the user's account:

const deleteUserAccount = () => {
    firebase.auth().currentUser.reauthenticateWithCredential(firebase.auth.EmailAuthProvider.credential(currentUser.email, reEnterPassword)).then(() => {
        firebase.firestore().collection("users").doc(firebase.auth().currentUser.uid).delete().then(() => {
            firebase.auth().currentUser.delete().then(() => {

            }).catch((error) => {
                console.log(error)
            })
        }).catch((error) => {
            console.log(error)
        })
      }).catch((error) => {
            console.log(error)
        })
}

This code only works for users that signed in with email and password, and trying to run it with an account that signed in with Google returns this error:

code: "auth/wrong-password" message: "The password is invalid or the user does not have a password."

What am I doing wrong and how do I modify it to accommodate both auth types?


Solution

  • If you signed in with Google (or any other social provider) the Firebase Authentication user account does not have a password associated with it. So to sign in to that account again, the user has to sign in with the same Google credentials as before.

    If you want to also allow the user to sign into that account with a password, you can set up a linked email+password account by lining email address and password credentials to the user account.