Skip to content

Instantly share code, notes, and snippets.

@wellingtonjhn
Created April 29, 2018 08:05
Show Gist options
  • Select an option

  • Save wellingtonjhn/d712b9f79141456563d522b29d719235 to your computer and use it in GitHub Desktop.

Select an option

Save wellingtonjhn/d712b9f79141456563d522b29d719235 to your computer and use it in GitHub Desktop.
AuthenticateUserHandler - MediatR
public class AuthenticateUserHandler : IRequestHandler<AuthenticateUser, Response>
{
private readonly IJwtService _jwtService;
private readonly IUserRepository _repository;
public AuthenticateUserHandler(IJwtService jwtService, IUserRepository repository)
{
_jwtService = jwtService;
_repository = repository;
}
public async Task<Response> Handle(AuthenticateUser request, CancellationToken cancellationToken)
{
var response = new Response();
var encodedPassword = new Password(request.Password).Encoded;
var user = await _repository.Authenticate(request.Email, encodedPassword);
if (user == null)
{
response.AddNotification(new Notification("user", "Usuário ou senha inválidos"));
return response;
}
var jwt = _jwtService.CreateJwtToken(user);
response.AddValue(jwt);
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment