wbhill13
wbhill13

Reputation: 75

How to create a firebase cloud function to reset a users password

I am working on an app, and part of the functionality of the app is having an admin login and be able to reset a user's password.

var functions = require('firebase-functions');
var admin = require("firebase-admin");
var serviceAccount = require("/removed-for-privacy.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://troop-30-elections-web-app.firebaseio.com"
});
    exports.resetPassword = functions.https.onCall((docId,newPass) => {
        console.log("step 2");
        admin.auth().updateUser(docId, {
            password: newPass,
        }).then(() => {
            const modal = document.querySelector('#modal-reset');
              M.Modal.getInstance(modal).close();
              resetForm.reset();
        });
    });

.

 resetForm.addEventListener('submit', (e) => {
            console.log("Step 1");
            e.preventDefault();
            let newPass = resetForm['reset-password'].value;
            const resetPasswordFunction = firebase.functions().httpsCallable('resetPassword');
            resetPasswordFunction(docId,newPass);
            console.log("Step 1.5");
        });

I tried submitting the reset password form, but nothing happened. I looked in the console. There was no error, but I did see step 1 and step 1.5.

Upvotes: 0

Views: 247

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317712

Cloud Functions code runs on backend servers and can't access the DOM of the web page that invokes it. This means you can't do things like document.querySelector. If the function requires information to run, you must pass that information from the client app as a parameter to the function.

Upvotes: 2

Related Questions