Skip to content

Commit 95d493b

Browse files
committed
Updates password hashing to be async
1 parent 3054942 commit 95d493b

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

server/controllers/users.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ async function login(req, res) {
4343
});
4444
}
4545

46-
return user.comparePassword(req.body.password, (err, isMatch) => {
47-
if (isMatch && !err) {
46+
return user.comparePassword(req.body.password, (match) => {
47+
if (match) {
4848
const token = jwt.sign(JSON.parse(JSON.stringify(
4949
{ userId: user.id, username: user.username },
5050
)), secretKey, { expiresIn: 86400 * 30 });

server/models/user.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ module.exports = (sequelize, DataTypes) => {
1313
},
1414
});
1515

16-
User.beforeSave((user) => {
16+
User.beforeSave(async (user) => {
1717
if (user.changed('password')) {
18-
user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null);
18+
user.password = await bcrypt.hash(user.password, 10);
1919
}
2020
});
2121

22-
function comparePassword(passw, cb) {
23-
bcrypt.compare(passw, this.password, (err, isMatch) => {
24-
if (err) {
25-
return cb(err);
26-
}
27-
return cb(null, isMatch);
28-
});
22+
async function comparePassword(password, cb) {
23+
const match = await bcrypt.compare(password, this.password);
24+
if (match) {
25+
return cb(match);
26+
}
27+
return cb(false);
2928
}
3029

3130
User.prototype.comparePassword = comparePassword;

0 commit comments

Comments
 (0)