Home > Software design >  Is there a library for promptForCredentials with firebase?
Is there a library for promptForCredentials with firebase?

Time:07-07

I am trying to learn how to delete users from the Firebase Auth provided, but when I click on the delete button on my html it gives me Uncaught ReferenceError: promptForCredentials is not defined I can't find any information on if I am suppose to import it anywhere. I'm using plain vanilla javascript.

Here is my code:

my html includes for scripts:

<script src="https://www.gstatic.com/firebasejs/8.2.7/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.7/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.7/firebase-database.js"></script>

javascript:

const firebaseConfig = {//my app information}
firebase.initializeApp(firebaseConfig);

const auth = firebase.auth();
const database = firebase.database();
const fname = document.querySelector('#fname');
        const lname = document.querySelector('#lname');
        const email = document.querySelector('#email');
        const password = document.querySelector('#password');
        const confirm_password = document.querySelector('#confirmpassword');
        const deleteConfirmed = document.querySelector('.deleteConfirmed');
        const passConfirmed = document.querySelector('#deletePass');

        auth.onAuthStateChanged(user => {
            if(user) {
                const user = auth.currentUser
                const database_ref = database.ref('users/'   user.uid).on('value', (snapshot) => {
                    fname.value = snapshot.val().first_name;
                    lname.value = snapshot.val().last_name;
                    email.value = snapshot.val().email;
                });

                deleteConfirmed.addEventListener('click', () => {
                    const credential = promptForCredentials();
                    user.reauthenticateWithCredential(credential).then(() => {
                      // User re-authenticated.
                    }).catch((error) => {
                      // An error ocurred
                      // ...
                    });
                })
                        
            } else {
                location.href = "/login";
            }
            
        });

CodePudding user response:

It looks like you copied code from the Firebase documentation on re-authenticating a user, which shows:

const user = firebase.auth().currentUser;

// TODO(you): prompt the user to re-provide their sign-in credentials
const credential = promptForCredentials();

user.reauthenticateWithCredential(credential).then(() => {
  // User re-authenticated.
}).catch((error) => {
  // An error occurred
  // ...
});

That TODO there means that you have to implement the prompt for the credentials of the user in whatever way works for your app, and then pass them to the reauthenticateWithCredential API.

  • Related