-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserverUtil.js
More file actions
68 lines (63 loc) · 2.98 KB
/
Copy pathserverUtil.js
File metadata and controls
68 lines (63 loc) · 2.98 KB
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
const jsSHA = require("jssha"); // package for hash operations
const nodemailer = require('nodemailer'); // package for sending emails
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'gathable@gmail.com',
pass: 'asdfg1234qwerty'
}
});
// check whether the received hash string is really a hash value
function checkPasswordHash(hash) {
var hashFormat = /[0-9a-f]{64}/
if (!hash.match(hashFormat)) {
return "ERROR: NOT A VALID SHA256 HASH"
}
else return "" // return empty string means success
}
// generate a random SHA256 hash for email or reset password
function randomHash() {
var hashObj = new jsSHA("SHA-256", "TEXT", { numRounds: 1 }) // numRounds: how many hash iterations, 1 is good enough for us
hashObj.update(Math.random().toString(16).substring(2))
return hashObj.getHash("HEX") // return hash value as a hexadecimal string
}
function groupHash(groupId) {
var hashObj = new jsSHA("SHA-256", "TEXT", { numRounds: 1 }) // numRounds: how many hash iterations, 1 is good enough for us
hashObj.update(Math.random().toString(16).substring(2) + groupId.toString())
return hashObj.getHash("HEX") // return hash value as a hexadecimal string
}
function newUserEmail(username, email, hash) {
var mailOptions = {
from: 'gathable@gmail.com',
to: email,
subject: 'Gathable E-mail Verification',
html: '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.0/font/bootstrap-icons.css">\
<h1>Welcome to <a class="navbar-brand"><i class="bi bi-calendar-range"></i> Gathable</a></h1>\
<h3>E-verification</h3><p>Click <a href=http://localhost:3100/verify/?uname=' + username + '&hash=' + hash + '> here</a> to verify your e-mail registered with us!</p>\
<p>We hope you enjoy your experience with us!</p><p>Thanks.</p>'
}
transporter.sendMail(mailOptions, function (error, info) {
if (error)
console.log(error)
else
console.log('Email sent');
});
}
function ResetPasswordEmail(username, email, hash) {
var mailOptions = {
from: 'gathable@gmail.com',
to: email,
subject: 'Gathable: Reset Password',
html: '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.0/font/bootstrap-icons.css">\
<h1>Thank you for using <a class="navbar-brand"><i class="bi bi-calendar-range"></i> Gathable</a></h1>\
<h3>Reset Password</h3><p>Click <a href=http://localhost:3100/reset/?uname=' + username + '&hash=' + hash + '> here</a> to reset your password.</p>\
<p>We hope you enjoy your experience with us!</p><p>Thanks.</p>'
}
transporter.sendMail(mailOptions, function (error, info) {
if (error)
console.log(error)
else
console.log('Email sent');
});
}
module.exports = { checkPasswordHash, randomHash, groupHash, newUserEmail, ResetPasswordEmail } // have to export like this