Implementing Firebase Change Password in Firebase Functions

463 views
Skip to first unread message

Sami Sami

unread,
Oct 29, 2020, 6:12:36 AM10/29/20
to Firebase Google Group
I'm trying to make users be able to change their password. currently I'm using what I found in the doc by google.

My problem now is how do I get the current user because I can't use currentUser in firebase functions.

Here's the code from the doc.

//////CODE FROM DOCUMENTATION
var user = firebase.auth().currentUser;
var newPassword = getASecureRandomPassword();

user.updatePassword(newPassword).then(function() {
  // Update successful.
}).catch(function(error) {
  // An error happened.
});


Here's my code


/////MY CODE IN F-FUNCTIONS

exports.changePassword = (req, res) => {
const password = {
newPassword: req.body.newPassword,
confirmNewPassword: req.body.confirmNewPassword
}
const { valid, errors } = ValidateChangePassword(password);
if (!valid) return response.status(400).json(errors);

firebase
.auth
.user(req.user.uid)
.updatePassword(newPassword)
.then(() => {
return console.log('email sent!')
})
.catch(error => {
return response.status(500).json({ error: error.code })
})
}


NOTE: I have used the original from google doc but still doesn't work also.

Arthur Thompson

unread,
Oct 30, 2020, 4:16:22 PM10/30/20
to fireba...@googlegroups.com
Hi Sami,

Your "changePassword" function does not have any special way of getting the user. Since it is an HTTP function you would have to explicitly pass in a "user" object which contains a "uid" property.

What you may want to use instead is a callable function where the client side Firebase SDK will fill in the calling user's data for you and on the function side you can retrieve the calling user's id with something like "context.auth.uid".

I hope that is what you are looking for.

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/4e092a02-ac13-44f2-b3d4-455ff4a4f3cbn%40googlegroups.com.

Sami Sami

unread,
Nov 1, 2020, 4:48:33 PM11/1/20
to fireba...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages