0
votes

I'm trying to use firebase-admin sdk to update my users passwords manually, the idea is to use a onCreate trigger to achieve this by creating a new document in firestore (with the right rules obviously).

According to firebase documentation i don't need to use anything else than this to autenticate from my firebase functions environment:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

In order to test the function i just manually added the document right from the firebase console ui for firestore, and as i can see the trigger is just doing it's work, the problem is when updatin the user password using the firebase-admin sdk.

I'm getting the next error message from the logs:

Error updating user: { Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token

this is the whole firebase cloud function if you want to see how it's implemented:

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

triggerNewDocument();

function triggerNewDocument() {
  exports.updateUserData = functions.firestore
    .document('updateUserPasswords/{userId}')
    .onCreate((snap, context) => {
      // Get an object representing the document
      // e.g. {'name': 'Marie', 'age': 66}
      const newValue = snap.data();
      console.log(newValue);

      // access a particular field as you would any JS property
      const uid = newValue.uid;
      const newPassword = newValue.password;
      return updateUserPassword(uid, newPassword);
      // perform desired operations ...
    });
}

function updateUserPassword(uid, newPassword) {
  admin.auth().updateUser(uid, {
    password: newPassword,
  })
    .then(function(userRecord) {
      // See the UserRecord reference doc for the contents of userRecord.
      return userRecord.toJSON();
    })
    .catch(function(error) {
      return error;
    });
}

Is there anything may i be missing here?, thanks in advance for any hint or help you could provide.

1

1 Answers

1
votes

The whole issue was somehow the service account autogenerated with the admin-sdk was inactive.

Anyway i had to disable and enable the service account at least 3 times to make it work, hopefully this can be helpful for anyone having the same issue.

A simple mistake