I am using NestJS and Passport to create a simple log in/registration form and I am using bcrypt to see if the password that has been hashed is matching the password that user provides in the login form, but it always returns true
JavaScript
x
10
10
1
async validateUser(username: string, pass: string): Promise<any> {
2
const user = await this.usersService.findOne(username);
3
if(user && bcrypt.compare('pass', user.password)) {
4
const { password, result } = user;
5
console.log(pass, user.password)
6
return result;
7
}
8
return null;
9
}
10
In the code above, even if I set the argument as a string it will return true and go inside the if statement, which should be false.
Advertisement
Answer
As the compare function of the returns a promise, you need to await it. Try this:
JavaScript
1
9
1
async validateUser(username: string, pass: string): Promise<any> {
2
const user = await this.usersService.findOne(username);
3
if (!user) return null;
4
const pwCheck = await bcrypt.compare('pass', user.password);
5
if (!pwCheck) return null;
6
const { password, result } = user;
7
return result;
8
}
9