-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangePassword.ts
105 lines (93 loc) · 2.93 KB
/
changePassword.ts
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
import { Router } from "express";
// import path from "path";
import { client } from "./db";
import { comparePassword, hashPassword } from "./hash";
export let passwordRouter = Router();
// passwordRouter.get("/changepassword", (req, res) => {
// res.sendFile(path.resolve("protected", "changepassword.html"));
// });
passwordRouter.post("/changepassword", async (req, res) => {
//console.log('hi: ', req.body)
let employeeName = req.body.employeeName;
let email = req.body.email;
let currentPassword = req.body.currentPassword;
let newPassword = req.body.newPassword;
let rePassword = req.body.rePassword;
//console.log(employeeName, email, currentPassword, newPassword, rePassword);
//res.sendFile(path.resolve("protected","changepassword.html"))
if (newPassword.length < 6) {
console.log("invalid new Password");
res.json({ error: "invalid new Password" });
return;
}
if (rePassword.length < 6) {
console.log("Re-type Password failed");
res.json({ error: "Re-type Password failed" });
return;
}
if (rePassword != newPassword) {
console.log("2 New Password Inputs should be Equal");
res.json({ error: "2 New Password Inputs should be Equal" });
return;
}
//try{
// await client.query(
// `
// SELECT * FROM employee
// WHERE name = $1 AND email = $2;
// `,
// [employeeName, email]
// );
// } catch {
// console.log("Incorrect Current Password");
// res.json({ error: "Incorrect Current Password" });
// }
let dbChecking = await client.query(
`
SELECT * FROM employee
WHERE name = $1 AND email = $2;
`,
[employeeName, email]
);
//console.log(dbChecking)
if (dbChecking.rows.length != 1) {
console.log("Incorrect Employee Name or Email");
res.json({ error: "Incorrect Employee Name or Email" });
return;
}
if ((dbChecking.rows.length === 1)) {
let dbRow = dbChecking.rows[0];
console.log(dbRow.password);
if (await comparePassword(currentPassword, dbRow.password)) {
let newHashPassword = await hashPassword(newPassword);
await client.query(
`
UPDATE employee
SET password = $1
WHERE name = $2 AND email = $3;
`,
[newHashPassword, employeeName, email]
);
console.log("ok: ", newPassword);
console.log("hashed: ", newHashPassword);
res.json({ error: "Success" });
}
else {
res.json({ error: "Incorrect Current Password" });
}
// if (!(await comparePassword(currentPassword, dbRow.password))) {
// res.json({ error: "Incorrect Current Password" });
// }
}
});
// passwordRouter.post("/logout", (req, res) => {
// console.log("logout");
// req.session.destroy((err) => {
// if (err) {
// console.log(err);
// }
// });
// console.log("destroy:", req.session);
// res.redirect("/");
// //res.json({});
// });