我使用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((函数中的Postapi获得响应。我需要得到响应来显示他/她的注册过程的用户状态
我更改了
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)
沃伊尔成功了。