Search code examples
meteormeteor-accounts

Meteor Accounts-ui to wait for email verification before user access


A Meteor web app on the local machine uses Accounts-password and Accounts-ui.
The user enters email and password in order to create a new account.

It needs to be configured so that the user gets a userId (thus become a currentUser) only after verifying the creation of the accounts via email.

The app has an smtp variable set up. It send the verification email fine. but it failed to prevent the user to 'login' before the verification. How can it be made so that it only gives a valid userId after the verification? Thanks

//server
Accounts.onCreateUser(function(options, user) {
  Accounts.config({
    sendVerificationEmail: true
  });
  return user;
});

Solution

  • I define a global helper:

    Template.registerHelper('isVerified',function(){ // return a true or false depending on whether the referenced user's email has been verified
      if ( Meteor.user() && Meteor.user().emails ) return Meteor.user().emails[0].verified; // look at the current user
      else return false;
    });
    

    And then in any template (typically my master template) I can do:

    {{#if isVerified}}
      content that only verified users should see
    {{else}}
      Please check your email for your verification link!
    {{/if}}