Refatoração do login page
This commit is contained in:
23
src/auth/users/change-password.service.ts
Normal file
23
src/auth/users/change-password.service.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { UserRepository } from '../users/UserRepository';
|
||||
import md5 = require('md5');
|
||||
|
||||
@Injectable()
|
||||
export class ChangePasswordService {
|
||||
constructor(private readonly userRepository: UserRepository) {}
|
||||
|
||||
async execute(userId: number, oldPassword: string, newPassword: string) {
|
||||
const current = await this.userRepository.findByIdAndPassword(
|
||||
userId,
|
||||
md5(oldPassword).toUpperCase(),
|
||||
);
|
||||
|
||||
if (!current) {
|
||||
throw new NotFoundException('Usuário não encontrado ou senha inválida');
|
||||
}
|
||||
|
||||
const newPasswordHash = md5(newPassword).toUpperCase();
|
||||
await this.userRepository.updatePassword(userId, newPasswordHash);
|
||||
return current;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user