Skip to content

Commit 8e6350c

Browse files
committed
check if pw exists before calling brcypt, add error msging
1 parent dd00029 commit 8e6350c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

server/models/user.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,16 @@ userSchema.set('toJSON', {
162162
userSchema.methods.comparePassword = async function comparePassword(
163163
candidatePassword
164164
) {
165-
return bcrypt.compare(candidatePassword, this.password);
165+
if (!this.password) {
166+
throw new Error('No password is set for this user.');
167+
}
168+
169+
try {
170+
return await bcrypt.compare(candidatePassword, this.password);
171+
} catch (error) {
172+
console.error('Password comparison failed!', error);
173+
return false;
174+
}
166175
};
167176

168177
/**

0 commit comments

Comments
 (0)