-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (100 loc) · 3.44 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//Databse : otp
//table : items
const express = require('express');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const path = require('path');
const mysql = require('mysql');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended : true }));
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout : 'main'}));
app.set('view engine', 'handlebars');
//Creating a twilio object. authToken value is to be replaced.
const accountSid = 'AC9d09f90bcdd9907522ab142acf8e084f';
const authToken = 'my_auth_token';
const client = require('twilio')(accountSid, authToken);
//Create database connection
const db = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'otp'
});
//Connect
db.connect((err) => {
if(err) throw err;
console.log("MySql connected");
});
//Create a database
app.get('/createdb', (req, res) => {
let sql = `CREATE DATABASE otp`;
db.query(sql, (err, results) => {
if(err) throw err;
console.log(results);
res.send("Database created successfully.");
});
});
//Create a table in the database
app.get('/createitemstable', (req, res) => {
let sql = `CREATE TABLE items(items_id int AUTO_INCREMENT, name VARCHAR(255), pass VARCHAR(255), PRIMARY KEY (items_id))`;
db.query(sql, (err, results) => {
if(err) throw err;
console.log(results);
res.send("Items table created");
});
});
//Generate OTP and update it to table, also render a form
app.get('/', (req, res) => {
res.render('home'); //Take a name and generate an OTP
});
app.post('/generate', (req, res) => {
//5-digit OTP
let number = 10000 + Math.floor(Math.random() * 89999);
console.log(number);
client.messages.create({
body: 'number.toString()',
from: '+12017786590',
to: '+91999999999'
}).then(message => console.log(message.sid));
let item = {name : req.body.name, pass : number};
let sql = `INSERT INTO items SET ?`;
db.query(sql, item, (err, results) => {
if(err) throw err;
console.log(results);
res.render('verify');
});
res.end()
});
//Verify the otp entered by the user. If valid then verification was successful and delete the entry from table
//since OTP means one-time password
app.post('/verify', (req, res) => {
let pin = req.body.pass;
let sql = `SELECT * FROM items WHERE pass = '${pin}'`;
db.query(sql, (err, results) => {
if(err){
throw err;
}
sql = `SELECT COUNT(*) as cnt FROM items WHERE pass = '${pin}'`;
db.query(sql, (err1, results1) => {
if(err1) throw err1;
//let obj = JSON.parse(JSON.stringify(results1));
if(results1[0].cnt != 1){
res.send("Invalid OTP");
console.log(results1[0].cnt);
}
else{
res.send("User Verification Complete.");
let sql1 = `DELETE FROM items WHERE pass = '${pin}'`;
db.query(sql1, (err2, results2) => {
if(err2) throw err2;
console.log(results2);
});
}
});
});
});
app.listen(3000, () => {
console.log("Server started at PORT 3000");
});