Fred J.
Fred J.

Reputation: 6019

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;
});

Upvotes: 2

Views: 426

Answers (3)

Tyler Shuster
Tyler Shuster

Reputation: 435

It's not quite as simple as all that. Meteor needs a user to have an ID in order to send them an email. What you want to do is probably not actually prevent a login, but prevent the In order to do what you want, you'll have to prevent them from seeing anything when they are logged in. Something like this in your top-level template:

{{#if userVerified}}
  // Body of your app here
  {{> Template.dynamic template=main}}
{{else}}
  You are not yet verified
{{/if}}

And this in the corresponding .js file:

Template.body.helpers({
  userVerified () {
    const user = Meteor.user();
    return user.emails[0].verified;
  }
});

Upvotes: 2

Michel Floyd
Michel Floyd

Reputation: 20227

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}}

Upvotes: 1

Adam
Adam

Reputation: 3288

As @Season has pointed out, the verification requires that a userId be created in order to link it to the verification email.

What you could do is prevent a user from logging in if they haven't verified their email. See this answer to another question which accomplishes that:

https://stackoverflow.com/a/24940581/3512709

Upvotes: 1

Related Questions