Skip to content

Commit 4001563

Browse files
committed
refactor: use await in updatePassword API
1 parent 101c616 commit 4001563

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

lib/index.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -629,25 +629,29 @@ class ErrsolePostgres extends EventEmitter {
629629
}
630630

631631
const query = 'SELECT * FROM errsole_users WHERE email = $1';
632-
return new Promise((resolve, reject) => {
633-
this.pool.query(query, [email], async (err, results) => {
634-
if (err) return reject(err);
635-
if (!results.rows.length) return reject(new Error('User not found.'));
632+
const { rows } = await this.pool.query(query, [email]);
636633

637-
const user = results.rows[0];
638-
const isPasswordCorrect = await bcrypt.compare(currentPassword, user.hashed_password);
639-
if (!isPasswordCorrect) return reject(new Error('Current password is incorrect.'));
634+
if (!rows.length) {
635+
throw new Error('User not found.');
636+
}
640637

641-
const hashedPassword = await bcrypt.hash(newPassword, 10);
642-
const updateQuery = 'UPDATE errsole_users SET hashed_password = $1 WHERE email = $2';
643-
this.pool.query(updateQuery, [hashedPassword, email], (err, updateResults) => {
644-
if (err) return reject(err);
645-
if (updateResults.rowCount === 0) return reject(new Error('Password update failed.'));
646-
delete user.hashed_password;
647-
resolve({ item: user });
648-
});
649-
});
650-
});
638+
const user = rows[0];
639+
const isPasswordCorrect = await bcrypt.compare(currentPassword, user.hashed_password);
640+
641+
if (!isPasswordCorrect) {
642+
throw new Error('Current password is incorrect.');
643+
}
644+
645+
const hashedPassword = await bcrypt.hash(newPassword, 10);
646+
const updateQuery = 'UPDATE errsole_users SET hashed_password = $1 WHERE email = $2';
647+
const updateResults = await this.pool.query(updateQuery, [hashedPassword, email]);
648+
649+
if (updateResults.rowCount === 0) {
650+
throw new Error('Password update failed.');
651+
}
652+
653+
delete user.hashed_password;
654+
return { item: user };
651655
}
652656

653657
/**

0 commit comments

Comments
 (0)