-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreg.js
46 lines (40 loc) · 1.8 KB
/
reg.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
const bcrypt = require("bcrypt")
const conn = require("./dbconnect")
const crypto = require("crypto")
const userReg = function (email, password, role, is_enabled) {
const select = `SELECT COUNT(*) AS cnt FROM users WHERE email = "${email}" LIMIT 1 `
conn.query(select, (error, result) => {
console.log('result')
if (result[0].cnt > 0) {
console.log('user with the email already exists')
} else {
const saltRound = 10;
bcrypt.genSalt(saltRound, function (err, salt) {
if (err) {
throw err
} else {
bcrypt.hash(password, salt, function (err, hash) {
if (err) {
throw err
} else { // reg user
const token = crypto.randomBytes(20).toString('hex') //save to user table on token column
timeStamp = Date.now() //save to user table on created_at colum
const insertUser = `INSERT INTO users(email,password,role,created_at, is_enabled,token)
VALUES('${email}', '${hash}', '${role}',${timeStamp},${is_enabled},'${token}')`;
conn.query(insertUser, (error, result) => {
if (error) {
console.log(error)
console.log('error occured')
} else {
console.log('new user registered')
}
})
}
})
}
})
}
})
}
module.exports = userReg;
// module.exports = emailConfirm;