-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (73 loc) · 2.57 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
var con = require('./connection');
const bodyParser = require('body-parser');
const crypto = require('crypto');
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'tothdngbgmmiswxb'
}
});
var express = require('express');
var bcrypt = require('bcrypt');
const saltRounds = 10;
var app = express();
app.use(bodyParser.json());
app.use("/assets",express.static("assets"));
app.use(bodyParser.urlencoded({extended: true}));
app.get('/',function(req,res)
{
res.sendFile(__dirname+'/register.html')
});
app.post('/',function(req,res){
var firstname = req.body.firstName;
var middlename = req.body.middleName;
var lastname = req.body.lastName;
var NID = req.body.nationalId;
var emaill = req.body.email;
var PN = req.body.phoneNumber;
var PW = generateRandomPassword(10);
console.log('Pass:',PW);
bcrypt.hash(PW, saltRounds, function(err, hash) {
if (err) throw err;
con.connect(function(error) {
if (error) throw error;
var sql = "INSERT INTO users(first_name,middle_name,last_name,national_id,email,phone_number,password) VALUES ('" + firstname + "','" + middlename + "','" + lastname + "','" + NID + "','" + emaill + "','" + PN + "','" + hash + "')";
con.query(sql, function(error, result) {
if (error) throw error;
res.send('RECORD SUCCESSFULL' + console.log("success"));
});
});
});
const mailOptions = {
from: '[email protected]',
to: emaill,
subject: 'E-voting security alert',
text: `Dear ${firstname},
We hope this email finds you well. As a user of E-voting site, we are writing to provide you with a new password to access the E-voting website. This password is needed for you to securely access through and use the features of the app.
Your password is as follows:
password : ${PW}
Please note that this password is automatically generated by our system.
Thank you for using E-voting.
Best regards,
E-voting Project
`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
res.status(500).send('Error sending email');
} else {
console.log('Email sent: ' + info.response);
res.send('Password reset email sent');
}
});
});
app.listen(5501);
function generateRandomPassword(length) {
return crypto.randomBytes(Math.ceil(length/2))
.toString('hex')
.slice(0,length);
}