Search code examples
javascriptfirebasefirebase-authenticationgoogle-cloud-functionsfirebase-admin

Cloud function to reset users password isn't working


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

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).then(() => {
            const modal = document.querySelector('#modal-reset');
          M.Modal.getInstance(modal).close();
          resetForm.reset();
        })
        console.log("Step 1.5");
    });

.

var functions = require('firebase-functions');
var admin = require("firebase-admin");
var serviceAccount = require("./troop-30-elections-web-app-firebase-adminsdk-obsmr-6913a9dca3.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,
    });
});

I tested it by filling out the form, and nothing happened. I looked in the console and there were no errors, but I saw Step 1 and Step 1.5.


Solution

  • I did more research and edited my code. This works:

    resetForm.addEventListener('submit', (e) => {
            console.log("Step 1");
            e.preventDefault();
            let newPass = resetForm['reset-password'].value;
            const resetPasswordFunction = firebase.functions().httpsCallable('resetPassword');
            resetPasswordFunction({docId: docId, newPass: newPass}).then(() => {
                const modal = document.querySelector('#modal-reset');
              M.Modal.getInstance(modal).close();
              resetForm.reset();
            });
            console.log("Step 1.5");
        });
    

    .

    var functions = require('firebase-functions');
    var admin = require("firebase-admin");
    var serviceAccount = require("./troop-30-elections-web-app-firebase-adminsdk-obsmr-6913a9dca3.json");
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://troop-30-elections-web-app.firebaseio.com"
    });
    
    exports.resetPassword = functions.https.onCall((data) => {
      return admin.auth().updateUser(data.docId, { 
        password: data.newPass
       })
      .then(() => {
          return {"text": "User Password Successfully Updated"};  // this is what gets sent back to the app
      });
    });