Can I reset a user's password using the Firebase Admin SDK for Node?

Viewed 12266

The docs from Firebase suggest that the API offers the same features as the console:

It is not always convenient to have to visit the Firebase console in order to manage your Firebase users. The admin user management API provides programmatic access to those same users. It even allows you to do things the Firebase console cannot, such as retrieving a user's full data and changing a user's password, email address or phone number.

But the reference docs don't list a function to reset a user's password. Am I missing something?

2 Answers

EDIT: This answer is now out of date, see Andrea's answer below for how to send a password reset link through the Firebase SDK.

It depends on which definition of 'reset' you're using.

If you mean reset as in 'change', then yes - the updateUser function allows you to provide a new password. See the following example from the docs:

admin.auth().updateUser(uid, {
  email: "modifiedUser@example.com",
  phoneNumber: "+11234567890",
  emailVerified: true,
  password: "newPassword",
  displayName: "Jane Doe",
  photoURL: "http://www.example.com/12345678/photo.png",
  disabled: true
})
  .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);
  });

If, on the other hand, you mean reset as in 'send a password reset email', then no, there doesn't seem to be a simple way of doing so via the Admin SDK.

Yes, you can. To generate a password reset link, you provide the existing user's email. Then you can use any email service you like to send the actual email. Link to documentation.

// Admin SDK API to generate the password reset link.
const userEmail = 'user@example.com';
admin.auth().generatePasswordResetLink(userEmail, actionCodeSettings)
  .then((link) => {
    // Construct password reset email template, embed the link and send
    // using custom SMTP server.
    return sendCustomPasswordResetEmail(email, displayName, link);
  })
.catch((error) => {
  // Some error occurred.
});
Related