Add nodemailer configuration and console.log statement

Update formAJAX function to log data from the server
Add getUserByEmail and checkEmail functions
Update profile.ejs to require login
Update addSensorData function to emit new sensor data
Update api.css with styling changes
Update token route to generate and send token email
Update authChecker middleware to allow user and token routes
This commit is contained in:
newtbot
2024-01-27 04:52:02 +08:00
parent 818d90b440
commit c234aa3616
15 changed files with 638 additions and 148 deletions

View File

@ -0,0 +1,82 @@
const { transporter } = require("../modules/nodemailer");
const path = require("path");
require("dotenv").config({ path: path.resolve(__dirname, "../.env") });
/*
var message = {
from: "sender@server.com",
to: "receiver@sender.com",
subject: "Message title",
text: "Plaintext version of the message",
html: "<p>HTML version of the message</p>",
};
//send mail with defined transport object
transporter.sendMail(data[, callback])
*/
async function sendContactEmail(email, name, message) {
console.log(email, name, message);
try {
let contactMessage = await transporter.sendMail({
to: process.env.euser,
subject: "Contact us Message",
html: `
<h1>Contact us Message</h1>
<p><strong>From:</strong> ${name}</p>
<p><strong>User Email:</strong> ${email}</p>
<p><strong>Message:</strong> ${message}</p>
<p>Thank you for contacting us. We will get back to you as soon as possible.</p>
<p>Regards,</p>
<p>EcoSaver Team</p>
<p><a href="https://ecosaver.teeseng.uk/">EcoSaver Website</a></p>
<p>Please do not reply to this email.</p>
`,
});
transporter.sendMail({ contactMessage }, function (error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
} catch (error) {
console.error(error);
}
}
async function sendTokenEmail(email, token) {
try {
let tokenMessage = await transporter.sendMail({
to: email,
from: process.env.euser,
subject: "API Token",
html: `
<h1>API Token</h1>
<p><strong>Token:</strong> ${token}</p>
<p>Please do not lose this token and do not share your token with anyone!</p>
<p>Thank you for using EcoSaver.</p>
<p>Regards,</p>
<p>EcoSaver Team</p>
<p><a href="https://ecosaver.teeseng.uk/">EcoSaver Website</a></p>
<p>Please do not reply to this email.</p>
`,
});
transporter.sendMail({ tokenMessage }, function (error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
} catch (error) {
console.error(error);
}
}
module.exports = { sendContactEmail , sendTokenEmail };

View File

@ -18,7 +18,6 @@ async function getSensorData() {
const sensorData = await sensorDataModel.findAll();
return sensorData;
}
let dataArray = [];
async function addSensorData(id_sensor, id_location, sensordata) {
const sensorData = await sensorDataModel.create({
sensorid: id_sensor,
@ -26,11 +25,10 @@ async function addSensorData(id_sensor, id_location, sensordata) {
measurement: sensordata.measurement,
});
//console.log("sensorData", sensorData);
//console.log("sensorData", sensordata);
dataArray.push(sensordata);
console.log("dataArray", dataArray);
//console.log("sensorData", sensordata.measurement);
io().emit('sensorData:new', dataArray)
io().emit('sensorData:new', sensordata)
return sensorData;
}
@ -68,7 +66,7 @@ async function getSensorDataById(id) {
async function getLatestData() {
const sensorData = await sensorDataModel.findAll({
limit: 1,
limit: 6,
order: [["createdAt", "DESC"]],
});
return sensorData;

View File

@ -20,6 +20,16 @@ async function getUserByID(userid) {
return userRes;
}
async function getUserByEmail(email) {
let userRes = await userModel.findOne({
where: {
email: email,
},
});
if (!userRes) return false;
return userRes;
}
//api/v0/auth/register
/* Registering new user
1) req.body is taken from html form or wtv
@ -131,9 +141,23 @@ async function updateProfile(user, body) {
}
}
async function checkEmail(email) {
let emailRes = await userModel.findOne({
where: {
email: email,
},
});
if (!emailRes) return false;
return true;
}
module.exports = {
getUserByID,
getUserByEmail,
addUser,
loginUser,
updateProfile,
checkEmail
};