Created
April 29, 2018 08:05
-
-
Save wellingtonjhn/d712b9f79141456563d522b29d719235 to your computer and use it in GitHub Desktop.
AuthenticateUserHandler - MediatR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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