user555
user555

Reputation: 1529

resetPassword issues in meteor

I sent enrollment email to the user and when he enters password and other details I'm trying to reset the password but it is throwing error

uncaught error extpected to find a document to change

enter image description here

As you can see in the mage

I've subscribed to the user record

my code

this.route('enroll', {
        path: '/enroll-account/:token',
        template: 'enroll_page',
        onBeforeAction: function() {
            Meteor.logout();
            Session.set('_resetPasswordToken', this.params.token);
            s = this.subscribe('enrolledUser', this.params.token).wait();
        }
    }),

After I'm displaying form and on the submit event

onSubmit: function(creds) {
            var options = {
                _id: Meteor.users.findOne()._id,
                name: creds.name
            }
            var token=Session.get('_resetPasswordToken');
            Meteor.call('updateUser', options, function(error, result) {
                if(!error) {
                    Accounts.resetPassword(token, creds.password, function(error) {
                        if (error) {
                            toastr.error("Sorry we could not update your password. Please try again.");
                            return false;
                        }
                        else{
                            toastr.error("Logged In");
                            Router.go('/');
                        }
                    });
                } else {
                    toastr.error("Sorry we could not update your password. Please try again.");
                    return false;
                }
            });
            this.resetForm();
            this.done();

            return false;
        }

Everything is working fine but resetpassword callback is not triggering and the above error is displaying in console.

my token is get deleted from the user record and I'm able to login using login form but

From the docs

Reset the password for a user using a token received in email. Logs the user in afterwards.

I'm not able to automatically login after resetting the password,above error is throwing

What am I missing here?

Upvotes: 4

Views: 1114

Answers (2)

corvid
corvid

Reputation: 11197

Alternatively, you could do something like as follows in your publication. This worked for me (but mine was a slightly more involved query than this).

Meteor.publish('enrolledUser', function (token) {
  check(token, String);
  return Meteor.users.find({
    $or: [{
      _id: this.userId
    }, {
      'services.password.reset.token': token
    }]
  });
});

From the docs, it says

Reset the password for a user using a token received in email. Logs the user in afterwards.

So basically, you have to subscribe to the logged in user after the fact as well. A little silly, but whatever.

Upvotes: 1

Sasikanth
Sasikanth

Reputation: 3043

this.subscribe('enrolledUser', this.params.token).wait();

here you're subscribing using resetPassword token

when you call Accounts.resetPassword method the method will reset the password and delete the token from user record.

So your subscription is lost and there are no records available in client side to modify (That is waht the error Expected to find a document to change)

Instead on first subscription save the user Id and subscribe to the user record using Id

so the subscription will not be lost

path: '/enroll-account/:token',
        template: 'enroll_page',
        onBeforeAction: function() {
            Meteor.logout();
            Session.set('_resetPasswordToken', this.params.token);
            s = this.subscribe('enrolledUser', this.params.token).wait();
        },
        onAfterAction:function(){
               if(this.ready()){
                  var userid=Meteor.users.findOne()._id;
                  Meteor.subscribe("userRecord",userid);
               }
        }

Upvotes: 5

Related Questions