流星电子邮件验证

Meteor email verification

我正在尝试使用本教程在我的项目中进行电子邮件验证: https://themeteorchef.com/tutorials/sign-up-with-email-verification

我有方法:

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

并在客户端调用此方法:

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

  }

我收到一封包含 link 的电子邮件。但是当我去 link 时,没有任何反应。 Meteor.user().emails[ 0 ].已验证 - 未变为真。

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

我没有收到成功文本。

我试过这个:

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

但是我好像做错了什么。

我在 Meteor / 后端方面不是很有经验...所以我真的希望能在这里找到一些帮助。想象一下 "Puss in Boots" 电影中的猫深深地注视着你的灵魂。现在就是我))

如何从

中删除"return"

return Accounts.sendVerificationEmail( userId )

然后重试。

您需要设置一个可以获取和验证验证令牌的路由。

类似于他们在您正在学习的教程中所做的事情 here

基本上,在前端获取验证令牌,以验证令牌作为参数调用帐户方法Accounts.verifyEmail

正如 Deepak 所建议的那样,在 React 路由器中应该是这样的:

<Route exact path='/reset-password/:token' component={ResetPasswordPage} />
<Route exact path='/verify-email/:token' component={VerifyEmailPage} />

VerifyEmailPage 可能如下所示:

import React, { Component } from 'react'

import { Accounts } from 'meteor/accounts-base'

export default class VerifyEmailPage extends Component {

  componentDidMount () {

    const token = this.props.match.params.token
    Accounts.verifyEmail(token, (err) => {
      if (err) {
        toastr.error('Could not verify email!', err.reason)
      } else {
        toastr.success('Email confirmed successfully!')
        this.props.history.push('/feeds')
      }
    })
  }

  render () {
    return (
      <div>{''}</div>
    )
  }
}