[英]Email is not sending through sendgrid and also not showing any error/ node.js
auth.js Whenever I try to register new account it's shows me an error 'message" unauthorized', i am not getting what exactly happen. auth.js 每当我尝试注册新帐户时,它都会向我显示一条错误消息“未经授权”,我不知道到底发生了什么。
exports.register = async (req, res) => {
try {
const { email } = req.body;
// Make sure this account doesn't already exist
const user = await User.findOne({ email });
if (user) return res.status(401).json({message: 'The email address you have entered is already associated with another account.'});
const newUser = new User({ ...req.body, role: "basic" });
const user_ = await newUser.save();
await sendVerificationEmail(user_, req, res);
} catch (error) {
res.status(500).json({success: false, message: error.message})
}
};
sending email for verification发送email进行验证
async function sendVerificationEmail(user, req, res){
try{
const token = user.generateVerificationToken();
// Save the verification token
await token.save();
let subject = "Account Verification Token";
let to = user.email;
let from = process.env.FROM_EMAIL;
let link="http://"+req.headers.host+"/api/auth/verify/"+token.token;
let html = `<p>Hi ${user.username}<p><br><p>Please click on the following <a href="${link}">link</a> to verify your account.</p>
<br><p>If you did not request this, please ignore this email.</p>`;
await sendEmail({to, from, subject, html});
res.status(200).json({message: 'A verification email has been sent to ' + user.email + '.'});
}catch (error) {
res.status(500).json({message: error.message})
}
}
Please if you find any solution reply as soon as possible TIA如果您找到任何解决方案,请尽快回复 TIA
I changed to nodemailer + sendgrid now it's working and use gmail service我更改为 nodemailer + sendgrid 现在它正在工作并使用 gmail 服务
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.