Email verification

I am trying to make an email verification in my project using this tutorial:
https://themeteorchef.com/tutorials/sign-up-with-email-verification

I’ve got method:

 //server/methods/send-email.js
    
    import { Meteor } from 'meteor/meteor';
    import { Email } from 'meteor/email';
    
    Meteor.methods({
      sendVerificationLink() {
        let userId = Meteor.userId();
        if ( userId ) {
          console.log("Email has been sent");
          return Accounts.sendVerificationEmail( userId );
        }
      }
    });

And call this method on client side:

   //client/form.js
    
    handleSubmit = () => {
        this.validateField('phone', this.state.phone)
        if (this.state.formValid) {
          this.update_User({phone: this.state.phone});
        }
    		Meteor.call( 'sendVerificationLink', ( error, response ) => {
              if ( error ) {
    						console.log(error);
              } else {
    						console.log("- There is no errors in Meteor.call -");
              }
            });
    
      }

I am getting an email with link. But when I go this link, nothing happens.
Meteor.user().emails[ 0 ].verified - doesn’t become true.

{Meteor.user().emails[ 0 ].verified ? <div>Success</div> : <div>Failed</div>}

I don’t get the Success text.

I’ve tried this:

  import { Meteor } from 'meteor/meteor';
    
    Accounts.emailTemplates.siteName = "name";
    Accounts.emailTemplates.from     = "name<admin@name.io>";
    
    Accounts.emailTemplates.verifyEmail = {
      subject() {
        return "[name] Verify Your Email Address";
      },
      text( user, url ) {
        let emailAddress   = user.emails[0].address,
            urlWithoutHash = url.replace( '#/', '' ),
            supportEmail   = "support@cryptocean.io",
            emailBody      = `To verify your email address (${emailAddress}) visit the following link:\n\n${urlWithoutHash}\n\n If you did not request this verification, please ignore this email. If you feel something is wrong, please contact our support team: ${supportEmail}.`;
    
        return emailBody;
      }
    };
    
    Accounts.onEmailVerificationLink = function() {
      console.log("Verified");
      user.emails[0].verified = true;
    }

But it seems I’m doing something wrong.

I am not very experienced in Meteor / backend… So I really hope to find some help here. Imagine cat looking deeply into your soul from “Puss in Boots” movie. That’s me right now))

I’d do some console.log()'s there to figure out what’s in Meteor.user(). It may be the server hasn’t updated verified yet. How does verified get updated?

I don’t think that’s correct…
You’re not even checking to see what the token value is or which account it’s related to.

Check the docs: Passwords | Meteor API Docs

There is a function you pass the token into for validation. :+1:t3:

    Accounts.onEmailVerificationLink = function() {
      console.log("Verified");
      user.emails[0].verified = true;
    }

However, I am not quite sure that I’m doing it right

@liveforblaze Hmm…

Check the Meteor code here for where verified is set to true.

Did you finish this section of the tutorial?

I’ve used the useraccounts package to handle this, but it’s getting old and unmaintained.

I’m guessing that’s a pretty solid tutorial. Double-check your work and don’t give up!

You’ll need to both verify the user’s token and call the done callback within onEmailVerificationLink:

Accounts.onEmailVerificationLink(function (token, done) {
  Accounts.verifyEmail(token, function (error) {
    if (error) {
      // handle the error, perhaps by showing the user a message about an invalid token
    }
    done();
  });
});

The verifyEmail function will take care of updating the users collection for you.

Keep in mind that when you modify the user object like this:

user.emails[0].verified = true;

you are updating the copy of the user that you fetched from the users collection. Changes you make to this copy won’t affect the collection. You need to use Meteor.users.update() to modify the user in a way that the server will see.

4 Likes

Thank you all guys. Now it’s ok. The problem was in router)

Glad that it solved your problem…I hope router works well now.