Accounts.onEnrollmentLink help


#1

I am trying to understand the lifecycle of account creation and the e-mails that can be sent. The first logical step was to define a callback for new accounts (short pseudo code)

Accounts.onCreateUser(function(options, user) {
 Accounts.sendVerificationEmail(user._id);
 return user;
});

which sends an e-mail to the user’s first unverified e-mail address with a link back to the site containing something like

/#/verify-email/lO_1rXOlnYtE_sb0iF9Z1e1ablcf9gKjDL7APbqX_FE

When the user clicks on the link in the e-mail, it would call Accounts.onEmailVerificationLink on the client.

And this is where I begin to get lost. According to the documentation it takes callback with two arguments, the token and a done function. What type of logic is supposed to be in this callback, and further more in the done function? Am I supposed to call Accounts.verifyEmail(token, [callback]) at this point? Or in the done function?

I would appreciate if someone could post a working example.

Last but not least there is the welcome e-mail sent with Accounts.sendEnrollmentEmail which contains a link

/#/enroll-account/6l00rIzc8SS9dp1jwZKdm8A1RVxZkv4_mEy8etARJIl

and the according callback Accounts.onEnrollmentLink. When is this appropriate? When in the lifecycle should the enrollment email be sent (before or after e-mail verification) and what does the enroll-account token accomplish?


#2

Hmm, either this is too trivial and I am stupid for not being able to figure this out, or nobody knows the answer?


#3

Do something like this, create a route first.

Router.route('Accounts.enrollment', {
  path: '/enroll-account/:token',
  template: 'Enrollment',
  onBeforeAction: function () {
    Session.set('_resetPasswordToken', this.params.token);
    this.next();
  },
  waitOn: function () {
    return Meteor.subscribe('Users.enrolledUser', this.params.token);
  },
  data: function () {
    return Meteor.users.findOne({
      "services.password.reset.token": this.params.token
    })
  }
})

Then let’s say you have an autoform for your password reset (omit because that’s easy).

Template['Enrollment'].events({
  'click button[type="submit"]': function (e, template) {
    if (AutoForm.validateForm('yourformid')) {
      var password = AutoForm.getFieldValue('yourformid', 'password');
      Accounts.resetPassword(Session.get('_resetPasswordToken'), password, function (err) {
        Router.go("somewhere else");
      })
    }
  }
});

#4

Thanks for replying. The password reset doesn’t seem to be the problem, it basically works out of the box. I was interested in the sign-up vs. e-mail verification cycle.


#5

Hi corvid, for some reason my route never loads. I just see the loading spinner.

I don’t see an error on the client or server re: the subscribe. Any suggestions?

Thanks!

EDIT: You might find this link useful… https://github.com/alex-shamray/blog/blob/9fccf4ab78408807f1f020d185b3614db3eb4881/Meteor/custom-meteor-enroll-template.md