async login(email: string, password: string) {
const user = await User.findOne({ where: { email } })
if (!user) {
throw ApiError.BadRequest('Your email or password was entered incorrectly.')
}
const isPassEquals = await bcrypt.compare(password, user.password)
if (!isPassEquals) {
throw ApiError.BadRequest('Your email or password was entered incorrectly.')
}
const userDto = new UserDto(user)
const tokens = tokenService.generateTokens(userDto.id, userDto.email)
await tokenService.saveToken(userDto.id, tokens.refreshToken)
return { ...tokens, user: userDto }
}
async findUserByEmailAndPassword(email: string, password: string): User {
const user = await User.findOne({ where: {email} });
if(user) {
const passwordValid = await bcrypt.compare(password, user.password);
if(passwordValid)
return user;
}
return null;
}
const user = await findUserByEmailAndPassword(email, password);
if(user === null)
throw ApiError.BadRequest('Your email or password was entered incorrectly.');
await bcrypt.compare(password, user.password)
затащить в if, но мне такой вариант не очень нравится. async login(email: string, password: string) {
const user = await User.findOne({ where: { email } })
var isPassEquals = false;
if (user) {
isPassEquals = await bcrypt.compare(password, user.password)
}
if (!user || !isPassEquals) {
throw ApiError.BadRequest('Your email or password was entered incorrectly.')
}
const userDto = new UserDto(user)
const tokens = tokenService.generateTokens(userDto.id, userDto.email)
await tokenService.saveToken(userDto.id, tokens.refreshToken)
return { ...tokens, user: userDto }
}