Reputation: 449
I'm using cloud functions, and I'm trying to manually override a users password (I also have password email resets set up also).
I can successfully update a user number, enable / disabled status etc however when passing the password variable it seems to fail, with no error message.
The password must be a 'raw, unhashed password. Must be at least six characters long.'
Following the documentation, my cloud function is as follows.
exports.resetUserPassword = functions.https.onCall(async (data, context) => {
if (!context.auth.token.superadmin) return
try {
console.log(data.password)
admin.auth().updateUser(data.uid, {
password: 'testpassword',
})
.then(function(userRecord) {
// See the UserRecord reference doc for the contents of userRecord.
console.log("Successfully updated user", userRecord.toJSON());
})
.catch(function(error) {
console.log("Error updating user:", error);
});
return true
} catch (error) {
console.log(error)
}
});
When I look at the logs for the firebase function, the response seems to indicate the password hasn't been updated (and certainly when I try to log in with the test account the password has been set to something...IDK....). Note that passwordHash and passwordSalt are undefined,
Successfully updated user { uid: 'HMxo5NcObYTN5LPsgSb0ZTCK6213',
email: '[email protected]',
emailVerified: false,
displayName: undefined,
photoURL: undefined,
phoneNumber: undefined,
disabled: false,
metadata:
{ lastSignInTime: 'Tue, 19 May 2020 00:36:16 GMT',
creationTime: 'Tue, 19 May 2020 00:36:16 GMT' },
passwordHash: undefined,
passwordSalt: undefined,
customClaims: { customer: true },
tokensValidAfterTime: 'Tue, 19 May 2020 13:50:02 GMT',
tenantId: undefined,
providerData:
[ { uid: '[email protected]',
displayName: undefined,
email: '[email protected]',
photoURL: undefined,
providerId: 'password',
phoneNumber: undefined } ] }
Upvotes: 1
Views: 560
Reputation: 317712
Your code is ignoring the promise returned by updateUser().then().catch()
. Callable functions need to return a promise that resolves with the response to send to the caller, after all the async work has finished. Right now, your function returns just true
and completes immediately, before the work of updateUser
actually finishes.
In order to allow the work to complete, you should return the promise chain, and indicate what the caller is supposed to receive:
return admin.auth().updateUser(...)
.then(() => {
return "whatever you want the client to receive on success"
})
.catch(error => {
return "whatever you want the client to receive on error"
})
Upvotes: 1