klanc klanc - 1 year ago 85
Javascript Question

Meteor.js - Fetch/Get Enrollment token (from Accounts.sendEnrollmentEmail)

I can't figure out how to get the enrollment token from the Accounts.sendEnrollmentEmail function.

I know this function sends a direct mail towards the user which in the end looks something like this:


However, I would need to somehow get only to the token part FCXzBbqHInZgBlLaOpu8Iv11jP9DJEG-e1auAHDsh6S as I want to send enrollment mail trough a different service (e.g Postmark)

How to do this?

Answer Source

The Accounts.sendEnrollmentEmail(userId, email) function generates a random token and saves it in the user's services.password.reset.token field.

The code that generates the token is:

  var token = Random.secret();
  var when = new Date();
  var tokenRecord = {
    token: token,
    email: email,
    when: when
  Meteor.users.update(userId, {$set: {
    "services.password.reset": tokenRecord

(You can view the function's source code here).

It then sends an email to the user using the Email package. If you want to use a different service to send the email, you basically have 2 options:

  • Use the same convention yourself (i.e, create the same record and use your own email service in your own function).
  • Use the existing function, allow the mail delivery to fail silently and then query the user's document for the token and send the email yourself.

Neither is a particularly good option, but both will work for the time being. I wish they had refactored this part into its own function.

Note that the accounts packages are expected to undergo some changes towards the release of the next Meteor versions.

BTW, this function is very similar to Accounts.sendResetPasswordEmail, which you may also wish to override or create your own version.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download