wbhill13
wbhill13

Reputation: 75

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.

Upvotes: 0

Views: 150

Answers (2)

wbhill13
wbhill13

Reputation: 75

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
  });
});

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317712

For callable type functions, you are obliged to return a promise that yields the value of the object to send back to the client app that invoked it. That promise must only resolve when all of the asynchronous work is complete, else the function will shut down before the work is complete.

Right now, your function is returning nothing, and ignoring the promise returned by updateUser(). Cloud Functions is terminating the function before the work is complete: the promise is being ignored. Your code should instead use that promise and send a result back to the client. This will send an empty object, but you can do whatever you want in it:

    return admin.auth().updateUser(docId, { password: newPass })
    .then(() => {
        return {};  // this is what gets sent back to the app
    });

Upvotes: 1

Related Questions