Skip to content

Commit 9c1b2de

Browse files
author
Simon Dupree
committed
promise meeeee hashPW and checkUser
1 parent a8315d0 commit 9c1b2de

File tree

3 files changed

+79
-48
lines changed

3 files changed

+79
-48
lines changed

src/bcrypt.js

+26-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
// const querystring = require('querystring');
22
const bcrypt = require('bcryptjs');
33

4-
const hashPassword = (password, callback) => {
5-
bcrypt.hash(password, 10, callback);
4+
const hashPassword = (password) => {
5+
return new Promise((resolve, reject) => {
6+
bcrypt.hash(password, 10, (err, hash) => {
7+
if (err) reject(err)
8+
resolve(hash)
9+
});
10+
})
611
};
712

8-
const comparePasswords = (password, hashedPassword, callback) => {
9-
bcrypt.compare(password, hashedPassword, function (err, res) {
10-
if (err) {
11-
console.log('booooooooooo')
12-
callback(err)
13-
} else {
14-
console.log('achievement')
15-
callback(null, res);
16-
}
17-
});
13+
const comparePasswords = (password, hash) => {
14+
return new Promise((resolve, reject) =>
15+
bcrypt.compare(password, hash, (err, res) => {
16+
if (err) reject(err)
17+
resolve(res)
18+
})
19+
)
1820
};
1921

22+
// const comparePasswords = (password, hashedPassword, callback) => {
23+
// bcrypt.compare(password, hashedPassword, function (err, res) {
24+
// if (err) {
25+
// console.log('booooooooooo')
26+
// callback(err)
27+
// } else {
28+
// console.log('achievement')
29+
// callback(null, res);
30+
// }
31+
// });
32+
// };
33+
2034

2135
module.exports = { hashPassword, comparePasswords }

src/dbQueries.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ const dbQuery = {
2828
},
2929
storeUser: function (username, admin, password, email, cb) {
3030
admin = false;
31-
hashPassword(password, (err, res) => {
32-
if (err) {
33-
console.log('storeuser pass hash error')
34-
} else {
31+
hashPassword(password)
32+
.then(res => {
3533
let query = `BEGIN;
3634
INSERT INTO users (username, is_admin, pass, email)
3735
VALUES ('${username}', '${admin}', '${res}', '${email}');
@@ -41,25 +39,22 @@ const dbQuery = {
4139
cb("Error!", null);
4240
}
4341
cb(null, res);
44-
});
45-
}
46-
});
42+
})
43+
}).catch(console.log)
4744
},
45+
4846
checkUser: function (name, password, cb) {
4947
let query = `SELECT * FROM users WHERE username='${name}'`;
5048
databaseConnection.query(query, (err, res) => {
5149
if (err) {
5250
// cb("Errorrrrrrrrrr!", null);
5351
console.log('errorrrrrr')
5452
} else {
55-
comparePasswords(password, res.rows[0].pass, (err, res) => {
56-
if (err) {
57-
cb("Terror!", null)
58-
} else {
59-
console.log(res)
60-
cb(null, res)
61-
}
62-
})
53+
comparePasswords(password, res.rows[0].pass)
54+
.then(res => {
55+
cb(null, res);
56+
})
57+
.catch(console.log);
6358
}
6459
});
6560
},

tests/test.bcrypt.js

+43-21
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,54 @@ tape('check tape is working', (t) => {
88
t.end();
99
});
1010

11-
tape('check bcrypt is hashing', (t) => {
12-
hashPassword(password, (err, res) => {
13-
t.equal(err, null, 'error should be null');
14-
t.equal(res.substring(0, 4), '$2a$');
15-
t.end();
16-
})
17-
});
11+
// tape('check bcrypt is hashing', (t) => {
12+
// hashPassword(password, (err, res) => {
13+
// t.equal(err, null, 'error should be null');
14+
// t.equal(res.substring(0, 4), '$2a$');
15+
// t.end();
16+
// })
17+
// });
1818

19-
tape('check passwords correct', (t) => {
20-
hashPassword(password, (err, res) => {
21-
t.equal(err, null, 'err should be nully');
22-
comparePasswords(password, res, (err, correct) => {
23-
t.equal(err, null, 'err should be very null')
24-
t.equal(correct, true);
19+
tape('check bcrypt promise is hashing', (t) => {
20+
hashPassword(password)
21+
.then(res => {
22+
t.equal(res.substring(0, 4), '$2a$');
2523
t.end();
2624
})
27-
})
25+
.catch(console.log)
2826
});
2927

28+
tape('check passwords correct', (t) => {
29+
hashPassword(password)
30+
.then(res => {
31+
comparePasswords(password, res)
32+
.then(res => {
33+
t.equal(res, true);
34+
t.end();
35+
})
36+
})
37+
.catch(console.log)
38+
})
39+
40+
41+
// tape('check passwords correct', (t) => {
42+
// hashPassword(password, (err, res) => {
43+
// t.equal(err, null, 'err should be nully');
44+
// comparePasswords(password, res, (err, correct) => {
45+
// t.equal(err, null, 'err should be very null')
46+
// t.equal(correct, true);
47+
// t.end();
48+
// })
49+
// })
50+
// });
51+
3052
tape('check passwords incorrectcorrect', (t) => {
31-
hashPassword(password, (err, res) => {
32-
t.equal(err, null, 'err should be nully');
33-
comparePasswords('notpassword', res, (err, correct) => {
34-
t.equal(err, null, 'err should be very null')
35-
t.equal(correct, false);
36-
t.end();
53+
hashPassword(password)
54+
.then(res => {
55+
comparePasswords('notpassword', res)
56+
.then(res => {
57+
t.equal(res, false);
58+
t.end();
59+
})
3760
})
38-
})
3961
});

0 commit comments

Comments
 (0)