How to make the credential argument for .reauthenticateWithCredential() in firebase authentication?

Viewed 790

To delete/edit the information of a user in firebase auth, you need to have recently logged in. This can be done with .reauthenticateWithCredential(). How can I make the credential for this (using email auth) in javascript? I have tried with (email, password) and ({email, password}), but no luck.

Code:

function reauth() {
        const credential = firebase.auth.EmailAuthProvider.credential(user.auth.email, password);

        user.reauthenticateWithCredential(credential).then(function() {
                firebase.firestore().collection('users').doc(uid).delete().then(() => {
                    user.delete().then(function() {
                        navigation.replace("Signup")
                    }).catch(function(error) {
                        console.log(error)
                    })
                }).catch((error) => {
                    console.log(error)
                })
        }).catch(function(error) {
            console.log(error)
        })
    }
1 Answers

This worked for me (Web v9)

import {
  EmailAuthProvider, getAuth, reauthenticateWithCredential,
} from 'firebase/auth';

const onReaAuth = () => {
  const passowrd = "from user input";
  const auth = getAuth();
  const { currentUser } = auth;
  const { email } = currentUser;
  const credential = EmailAuthProvider.credential(email, password);
  
  reauthenticateWithCredential(currentUser, credential)
   .then(() => {
     console.log('done')
    }
 }
Related