React和Loopback 3验证用户电子邮件

时间:2018-12-03 17:59:26

标签: javascript reactjs react-redux loopback email-verification

我使用React和Loopback 3构建了一个注册组件。起初,我没有使用电子邮件验证。在下面的代码中,我从rest api得到了有关用户创建的响应。

export function signUpUser(user) {
  let userEmail = user.email;
  let userPassword = user.password;
  let userName = user.userName;
  let privilege="user"
  let credentials = {
    email: userEmail,
    password: userPassword,
    userName: userName,
    privilege:privilege
  };
  //console.log(credentials);
  return dispatch => {
    axios
      .request({
        method: "post",
        url: loopBack + "/blogusers",
        data: credentials
      })
      .then(response => {
        console.log(response)
        return dispatch(signUpSuccess(response.data));
      })
     .catch(error => {
        return dispatch(signUpFailure(error));
      });
  };
}

我的用户模型js是

"use strict";
module.exports = function(Bloguser) {

};

添加钩子以创建api后,我的用户模型js就是这样。

"use strict";
var config = require("../../server/config.json");
var path = require("path");
var senderAddress = "jp6t63je22rfqgf7@ethereal.email";

module.exports = function(Bloguser) {
  //send verification email after registration.

  Bloguser.afterRemote("create", function(context, userInstance, next) {
    console.log(">user.afterRemote triggered");
    var options = {
      type: "email",
      to: userInstance.email,
      from: senderAddress,
      subject: "Thanks for registering.",
      template: path.resolve(__dirname, "../../server/views/verify.ejs"),
      Bloguser: userInstance
    };
    userInstance.verify(options, function(err, response) {
      if (err) {
        Bloguser.deleteById(userInstance.id);
        return next(err);
      }
      console.log(">verification email sent.", response);
      context.res.render("response", {
        title: "Signed up successfully",
        content:
          "Please check your email and click on the verification link" +
          "before logging in.",
        redirectTo: "/login",
        redirectToLinkText: "Log in"
      });
    });
  });
};

通过此代码,我可以成功发送验证电子邮件,并且可以通过电子邮件中的链接验证用户。但是,我无法从signUpUser()函数中的Post api获得响应。我需要回复以显示他/她的注册过程的用户状态

1 个答案:

答案 0 :(得分:0)

我改变了

context.res.render("response", {
    title: "Signed up successfully",
    content:
      "Please check your email and click on the verification      link" +
      "before logging in.",
    redirectTo: "/login",
    redirectToLinkText: "Log in"
  });

context.res.status(200).send(userInstance)

Voile奏效了。