mysql - 为什么我在数据库中的密码不等于请求正文中的密码?
问题描述
我尝试在 Nestjs 中为我的 API 进行登录,因此当用户通过请求正文发送数据时,我捕获数据并使用 typeorm 的查询构建器,然后在确认是否用户存在我创建一个新的比较块,我不知道代码不起作用的原因,如果我使用https://bcrypt-generator.com/比较数据库中的哈希密码和请求正文的密码,这是真的,但在我的代码中它不起作用
async login(userRO: UserRO) {
const { email, password } = userRO;
const user = await getRepository(User)
.createQueryBuilder('user')
.where('user.email = :email', {email})
.getOne();
if (!user) {
throw new HttpException(
'Usuario no es correcto',
HttpStatus.BAD_REQUEST,
);
}
// hashPassword = $2y$12$ZvWFRLVoS2gxyCjLkCbOZuN7NKfYrpT6cWxSJaeiVr0PnPBeoI8GS
// password = pepito09
const pass = await bcrypt.compare(password, user.password);
if (!pass) { // this always throw an error
throw new HttpException(
'Contraseña incorrecta',
HttpStatus.BAD_REQUEST,
);
}
const rol = await getRepository(Rol)
.createQueryBuilder('rol')
.select('rol.name')
.leftJoinAndSelect(User, 'user', 'user.rolId = rol.id')
.where('user.email = :email', { email })
.getOne();
if (!rol) {
throw new HttpException(
'Rol no encontrado',
HttpStatus.NOT_FOUND,
);
}
const type = this.typeUser(rol.name) ;
const payload = { email: user.email, id: user.id, rol: rol.name };
return {
access_token: this.jwtService.sign(payload),
type,
};
}
因此,如果数据库中的密码和请求正文中的密码相等,我希望关于密码的比较块抛出 true,否则返回 false。此刻,总是抛出 true
解决方案
推荐阅读
- nginx - 在单个服务器块中为不同网站添加多个 NGINX 反向代理
- javascript - 是否有必要在按钮单击时清除超时反应?
- arrays - 在 txt 文件中使用 fscanf 并将值保存到表中
- r - 根据R中的每一行统计一定时间范围内的行数(tidyverse)
- java - 如何避免在@XmlType propOrder 中添加所有字段并从父类导入字段
- php - 通过作曲家下载 laravel/nexmo 时出错
- pact-broker - swagger-mock-validator 和 pactflow.io
- java - Optaplanner 不会改变计划变量
- php - 在 WordPress 页面编辑器中,如何添加 HTML 以将短代码提取的数据放在 2 列中?
- django - 为什么静态文件在 Django 中不起作用?