-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.js
76 lines (69 loc) · 2.2 KB
/
functions.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
const bcrypt = require('bcryptjs')
const nodemailer = require('nodemailer')
const crypto = require('crypto')
const { EMAIL, PASS } = require('./config/env')
const transporter = nodemailer.createTransport({
host: 'smtp.zoho.com',
port: 465,
secure: true,
auth: {
user: EMAIL,
pass: PASS,
},
})
module.exports = {
hash: str => {
return new Promise((resolve, reject) => {
bcrypt.hash(str, 15, function (err, hash) {
if (err) reject(err);
else resolve(hash);
})
})
},
compare: (plainText, hash) => {
return new Promise((resolve, reject) => {
bcrypt.compare(plainText, hash, function (err, match) {
if (err) reject(err);
else resolve(match);
})
})
},
sendMail: mailOptions => {
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (err, info) => {
if(err) reject(err);
else resolve(info);
});
})
},
randomString: () => {
return new Promise((resolve, reject) => {
crypto.randomBytes(35, (err, buffer) => {
if (err) reject(err)
else resolve(buffer.toString('base64'))
})
})
},
genMail: (data, type, recieverEmail, recieverName) => {
const email = {
from: EMAIL,
to: recieverEmail
}
if (type === 'confirm') {
email.subject = `Confirm your buzzybee account ${recieverName}!`
let URL = `https://www.buzzybee.io/verifyAccount/${data}`
email.text = `Please go to this link ${URL} to confirm your account`
email.html = `<b>Confirm your account!</b> <br/> Go <a href="${URL}">HERE</a> to confirm your account`
} else if (type === 'resend confirm') {
email.subject = `Confirm your buzzybee account ${recieverName}!`
let URL = `https://www.buzzybee.io/verifyAccount/${data}`
email.text = `Please go to this link ${URL} to confirm your account`
email.html = `<b>Confirm your account!</b> <br/> Go <a href="${URL}">HERE</a> to confirm your account <br/> Sorry for the trouble!`
} else {
email.subject = 'Your new password'
email.text = `This is your new password: ${data}`
email.html = `Your new password is: <br/> <b>${data}</b>`
}
return email
}
}