我正在尝试在注册时发送验证电子邮件。如果验证工作,列&;activated&;将更改为"true"。
我可以成功发送邮件,但我认为验证码有问题。
当我按下发送到电子邮件的链接时,我得到了"禁止"!我需要一些帮助!提前感谢!
app.post('/insertuser', function (_req, res) {
var data = JSON.parse(_req.body.data);
var username = data.username;
var age = data.age;
var password = data.password;
var fname = data.fname;
var lname = data.lname;
var address = data.address;
var city = data.city;
var email = data.email;
var sq = data.sq;
var answer = data.answer;
var pnumber = data.pnumber;
var dataentered = data.dataentered;
var date = new Date();
var mail = {
"id": username,
"created": date.toString()
}
secret_code = sha1(pnumber) //since pnumber is unique;
const token_mail_verification = jwt.sign(mail, secret_code, { expiresIn: '1d' });
var url = "http://localhost:3000/verify?username=" + token_mail_verification;
mysqlConnection.connect(function () {
var query = "Insert into Customer (Username,Age,Password,First_Name,Last_Name,Email,Address,City,Phone_No,SQ,Answer,Date_Entered) values('" + username + "','" + age + "','" + sha1(password) + "','" + fname + "','" + lname + "','" + email + "','" + address + "','" + city + "','" + pnumber + "','" + sq + "','" + answer + "','" + dataentered + "')";
mysqlConnection.query(query, function (err, results, _fields) {
if (err) {
console.log(err);
res.send('Please try again!');
}
else {
if (results.affectedRows > 0) {
var mailOptions = {
from: '//myemail',
to: email,
subject: "Account Verification",
text: "Click on the link below to veriy your account " + url,
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
//Handle error here
res.send('Please try again!');
} else {
console.log('Email sent: ' + info.response);
res.send('Thanks for registering! Please confirm your email! We have sent a link!');
}
});
}
else {
console.log("Try again");
res.send('Please try again!');
}
}
})
})
});
验证代码app.get('/verify', function (req, res) {
token = req.query.id;
console.log(token)
if (token) {
try {
jwt.verify(token, secret_code, (e, decoded) => {
if (e) {
console.log(e)
return res.sendStatus(403)
} else {
id = decoded.id;
mysqlConnection.connect(function () {
var query = " UPDATE Customers SET Activation = 'True' ; WHERE Email =" + email;
mysqlConnection.query(query, function (err, results, _fields) {
if (err) {
console.log(err);
res.send('Please try again!');
}
else {
console.log("updated Successfully");
}
})
})
}
});
} catch (err) {
console.log(err)
return res.sendStatus(403)
}
} else {
return res.sendStatus(403)
}
});```
您的查询参数命名为username而不是id,因此代码应该像这样
app.get('/verify', function (req, res) {
token = req.query.username;
在发送电子邮件之前,您可以将用户电子邮件添加到编码正文
var mail = {
username: username,
email: email,
created: date.toString()
}
,然后你可以解码它来查询用户信息或更新它,else块可以像这样
var email = decoded.email;
mysqlConnection.connect(function () {
var query =
" UPDATE Customers SET Activation = 'True' ; WHERE Email =" +
email;
mysqlConnection.query(query, function (err, results, _fields) {
if (err) {
console.log(err);
res.send("Please try again!");
} else {
console.log("updated Successfully");
}
});
});