-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.js
69 lines (58 loc) · 1.83 KB
/
mail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const nodemailer = require("nodemailer");
const express = require("express");
const bodyParser = require("body-parser");
require("dotenv").config();
const crypto = require("crypto");
const db = require("./db");
const transporterConfig = {
service: "gmail",
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD,
},
};
const port = 3000;
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
const transporter = nodemailer.createTransport(transporterConfig);
function generateToken() {
return crypto.randomBytes(20).toString("hex");
}
app.post("/reset-link", (req, res) => {
const userEmail = req.body.email;
if (!db.isEmailExisting()) {
return res.status(400).send("Email does not exist");
}
const resetToken = generateToken();
db.storeToken(userEmail, resetToken);
var host = req.get("host");
const resetLink = `http://${host}/reset-password?token=${resetToken}`;
const mailOptions = {
from: process.env.EMAIL,
to: userEmail,
subject: "Password Reset",
text: `Click the following link to reset your password: ${resetLink}`,
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return res.status(500).send(error.toString());
}
console.log("Email sent: " + info.response);
res.status(200).send("Password reset email sent successfully!");
});
});
app.get("/reset-password", (req, res) => {
res.sendFile("/public/reset.html", { root: __dirname });
});
app.post("/reset-password", (req, res) => {
db.setPassword(req.body.token, req.body.password);
res.status(200).send("Password reset successfully!");
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
app.get("/", (req, res) => {
res.sendFile("/public/index.html", { root: __dirname });
});