Search code examples
meteormeteor-accountsmeteor-methods

Accounts.changePassword doesn't work in Method


I'm working on a Meteor app, and I want to move a call to Accounts.changePassword from the client to a method. However, when I try to run the method, I get this server error :

Exception while invoking method 'setPersonalPassword' TypeError: Object [object Object] has no method 'changePassword'

This is the offending code:

'setPersonalPassword': function(oldPassword, newPassword){
    Accounts.changePassword(oldPassword, newPassword);
},

However, this code works fine on the client:

'submit form': function (event) {
    event.preventDefault();
    var oldPassword = event.target.oldPassword.value;
    var newPassword = event.target.newPassword.value;
    Accounts.changePassword(oldPassword, newPassword)
}

I'm particularly confused because I've successfully used both Accounts.createUser() and Accounts.setPassword() in other methods.


Solution

  • On the server you should use Accounts.setPassword which you can use with the current userId from the method invocation:

    Meteor.methods({
      'setPersonalPassword': function (newPassword){
        const userId = this.userId
        Accounts.changePassword(userId, newPassword)
      }
    },
    

    See: https://docs.meteor.com/api/passwords.html#Accounts-setPassword