Refactor JWT token generation and update password handling logic; add exception handling behavior

This commit is contained in:
masoodafar-web
2025-11-13 21:40:14 +03:30
parent 4b1b135065
commit 8c4b1ab4f4
15 changed files with 199 additions and 34 deletions
@@ -3,15 +3,50 @@ namespace CMSMicroservice.Application.UserCQ.Commands.SetPasswordForUser;
public class SetPasswordForUserCommandHandler : IRequestHandler<SetPasswordForUserCommand, Unit>
{
private readonly IApplicationDbContext _context;
private readonly IHashService _hashService;
public SetPasswordForUserCommandHandler(IApplicationDbContext context)
public SetPasswordForUserCommandHandler(IApplicationDbContext context, IHashService hashService)
{
_context = context;
_hashService = hashService;
}
public async Task<Unit> Handle(SetPasswordForUserCommand request, CancellationToken cancellationToken)
{
//TODO: Implement your business logic
return new Unit();
// basic validations
if (!string.Equals(request.NewPassword, request.ConfirmPassword, StringComparison.Ordinal))
{
throw new CMSMicroservice.Application.Common.Exceptions.ValidationException(new[]
{
new FluentValidation.Results.ValidationFailure(nameof(request.ConfirmPassword), "کلمه عبور و تایید آن یکسان نیستند.")
});
}
var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == request.UserId, cancellationToken)
?? throw new NotFoundException(nameof(User), request.UserId);
var hasExistingPassword = !string.IsNullOrWhiteSpace(user.HashPassword);
if (hasExistingPassword)
{
if (string.IsNullOrWhiteSpace(request.CurrentPassword))
{
throw new CMSMicroservice.Application.Common.Exceptions.ValidationException(new[]
{
new FluentValidation.Results.ValidationFailure(nameof(request.CurrentPassword), "کلمه عبور فعلی الزامی است.")
});
}
if (!_hashService.VerifyPassword(request.CurrentPassword, user.HashPassword))
{
throw new UnauthorizedAccessException("کلمه عبور فعلی نادرست است.");
}
}
// set new password (PBKDF2)
user.HashPassword = _hashService.HashPassword(request.NewPassword);
_context.Users.Update(user);
user.AddDomainEvent(new CMSMicroservice.Domain.Events.SetPasswordForUserEvent(user));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}