-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathLoginUseCase.cs
44 lines (41 loc) · 1.65 KB
/
LoginUseCase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Threading.Tasks;
using Web.Api.Core.Dto;
using Web.Api.Core.Dto.UseCaseRequests;
using Web.Api.Core.Dto.UseCaseResponses;
using Web.Api.Core.Interfaces;
using Web.Api.Core.Interfaces.Gateways.Repositories;
using Web.Api.Core.Interfaces.Services;
using Web.Api.Core.Interfaces.UseCases;
namespace Web.Api.Core.UseCases
{
public sealed class LoginUseCase : ILoginUseCase
{
private readonly IUserRepository _userRepository;
private readonly IJwtFactory _jwtFactory;
public LoginUseCase(IUserRepository userRepository, IJwtFactory jwtFactory)
{
_userRepository = userRepository;
_jwtFactory = jwtFactory;
}
public async Task<bool> Handle(LoginRequest message, IOutputPort<LoginResponse> outputPort)
{
if (!string.IsNullOrEmpty(message.UserName) && !string.IsNullOrEmpty(message.Password))
{
// confirm we have a user with the given name
var user = await _userRepository.FindByName(message.UserName);
if (user != null)
{
// validate password
if (await _userRepository.CheckPassword(user, message.Password))
{
// generate token
outputPort.Handle(new LoginResponse(await _jwtFactory.GenerateEncodedToken(user.Id, user.UserName),true));
return true;
}
}
}
outputPort.Handle(new LoginResponse(new[] { new Error("login_failure", "Invalid username or password.") }));
return false;
}
}
}