Repository navigation
Expand file tree
/
Copy pathUser.js
More file actions
138 lines (122 loc) · 3.63 KB
/
Copy pathUser.js
File metadata and controls
138 lines (122 loc) · 3.63 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
module.exports = {
registerUser,
signInUser,
editUser
}
const bcrypt = require('bcrypt');
const e = require('cors');
const saltRounds = 10;
async function registerUser(req, res, pool) {
let conn;
try {
let data = req.body
const hash = bcrypt.hashSync(data.password, saltRounds);
conn = await pool.getConnection();
let result = await conn.execute(
`insert into users values (:name, :email, :password)`,
[data.name, data.email, hash]
);
if(result.rowsAffected === 1){
res.sendStatus(200);
}
} catch (err) {
if(err.message === `ORA-00001: unique constraint (TESTMOVIE.USER_PK) violated`){
res.status(400).json("email already entered");
}
else{
res.status(400).json("registration failed");
}
console.log('Ouch!', err)
} finally {
if (conn) { // conn assignment worked, need to close
await conn.close()
}
}
}
async function signInUser(req, res, pool) {
let conn;
try {
let data = req.body;
conn = await pool.getConnection();
let result = await conn.execute(
`select * from users where email = :email`,
[data.email]
);
if(result.rows.length === 0){
res.status(401).json("account not found");
}
else{
bcrypt.compare(data.password, result.rows[0][2], function(err, valid) {
if(valid){
res.json({name: result.rows[0][0], email: result.rows[0][1]});
}
else{
res.status(401).json("wrong password");
}
});
}
} catch (err) {
res.status(400).json("authentication error");
console.log('Ouch!', err)
} finally {
if (conn) { // conn assignment worked, need to close
await conn.close()
}
}
}
async function editUser(req, res, pool) {
let conn;
try {
console.log(req.body)
let data = req.body;
console.log(data);
conn = await pool.getConnection();
let result = await conn.execute(
`select password from users where email = :email`,
[data.current_email]
);
if(bcrypt.compareSync(data.confirmpass, result.rows[0][0])){
if(!data.password.new){
console.log('not pass change');
await conn.execute(
`UPDATE users
SET name = :name, email = :email, password = :password
where email = :currentmail`,
{
name: {val: data.name},
email: {val: data.email},
password: {val: result.rows[0][0]},
currentmail: {val: data.current_email}
}
);
}
else{
console.log('new passs');
const hash = bcrypt.hashSync(data.password.value, saltRounds);
await conn.execute(
`update users
set name = :name, email = :email, password = :hash
where email = :currentemail`,
[data.name, data.email, hash, data.current_email],
);
}
res.status(200).json('success');
console.log('success');
}
else{
res.status(401).json("wrong password");
}
} catch (err) {
if(err.message === `ORA-00001: unique constraint (TESTMOVIE.USER_PK) violated`){
res.status(400).json("email already entered");
}
else{
res.status(400).json("update error");
}
console.log('Ouch!', err)
} finally {
if (conn) { // conn assignment worked, need to close
await conn.close()
}
}
}