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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (52)

我使用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的响应。我需要得到回复以显示他/她注册过程的用户状态

提问于
用户回答回答于
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有效。

扫码关注云+社区

领取腾讯云代金券