-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (89 loc) · 3.31 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
const nodemailer = require('nodemailer');
const twilio = require('twilio')
const fs = require('fs');
const { Vonage } = require('@vonage/server-sdk');
class CodeStore {
storeCode(id, code) {
throw new Error('Not implemented');
}
verifyCode(id, code) {
throw new Error('Not implemented');
}
}
class JsonFileCodeStore extends CodeStore {
storeCode(id, code) {
let codes = JSON.parse(fs.readFileSync('codes.json', 'utf8'));
codes[id] = code;
fs.writeFileSync('codes.json', JSON.stringify(codes));
}
verifyCode(id, code) {
let codes = JSON.parse(fs.readFileSync('codes.json', 'utf8'));
let isVerified = false;
if (codes[id] === code) {
isVerified = true;
delete codes[id];
fs.writeFileSync('codes.json', JSON.stringify(codes));
}
return isVerified;
}
}
function createTransporter(host, port, secure, user, password, codeStore = new JsonFileCodeStore()) {
return nodemailer.createTransport({
host: host,
port: port,
secure: secure,
auth: {
user: user,
pass: password
}
});
}
function sendMail(transporter, from, to, title, message, endmessage, codeStore) {
let code = Math.floor(Math.random() * 1000000);
transporter.sendMail({
from: from,
to: to,
html: '<html><head><title>' + title + '</title><style>body {font-family: Arial, sans-serif; text-align: center;}.container {width: 80%; margin: auto; background-color: #f2f2f2; padding: 20px; border-radius: 5px; text-align: center;}.code {font-size: 20px; color: #333; font-weight: bold; border: 2px solid blue; padding: 10px;}.footer {background-color: blue; color:white; padding :20px;width :80%;margin:auto;border-radius :5px;text-align:center;}</style></head><body><div class="container"><h2>' + title + '</h2><p>' + message + '</p><p class="code">' + code + '</p><p>' + endmessage + '</p></div><div class="footer"><a href="https://github.com/FlanZCode" style="color:white;">Made by FlanZ❤️</a></div></body></html>'
}, (error) => {
if (error) {
console.log(error);
} else {
codeStore.storeCode(to, code);
}
});
}
function sendTwilioMessage(SID, token, from, to, codeStore) {
let code = Math.floor(Math.random() * 1000000);
const client = new twilio(SID, token)
client.messages.create({
body:"Here is your code: " + code, from: from, to: to
}, (error) => {
if (error) {
console.log(error);
} else {
codeStore.storeCode(to, code);
}
});
}
function sendVonageMessage(applicationId, privateKey, from, to, codeStore) {
let code = Math.floor(Math.random() * 1000000);
const vonage = new Vonage({
applicationId: applicationId,
privateKey: privateKey
})
vonage.sms.send({
from: from,
to: to,
text: "Here is your code: " + code
}, (error) => {
if (error) {
console.log(error);
} else {
codeStore.storeCode(to, code);
}
});
}
function verifyCode(id, code, codeStore) {
return codeStore.verifyCode(id, code);
}
module.exports = { createTransporter, sendMail, sendTwilioMessage, sendVonageMessage, verifyCode, CodeStore, JsonFileCodeStore };