| | | 1 | | using KT.Domain.Ports; |
| | | 2 | | using KT.Modules.Security.Core.Application.Contracts; |
| | | 3 | | using KT.Modules.Security.Presentation.Contracts; |
| | | 4 | | using KT.Modules.Security.Presentation.Dto; |
| | | 5 | | |
| | | 6 | | namespace KT.Application |
| | | 7 | | { |
| | 6 | 8 | | internal class LoginService(IUserRepository userRepository, IAccessTokenService accessTokenService) : ILoginService |
| | | 9 | | { |
| | | 10 | | public async Task<TokenResponseDto> LoginAsync(string userName, string password) |
| | 6 | 11 | | { |
| | 6 | 12 | | if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) |
| | 3 | 13 | | throw new ArgumentException("Not all data was completed"); |
| | 3 | 14 | | var userList = await userRepository.SearchUsersAsync(userName, null); |
| | 3 | 15 | | var user = userList.FirstOrDefault(); |
| | 3 | 16 | | if (user == null || !BCrypt.Net.BCrypt.Verify(password, user.PasswordHash)) |
| | 2 | 17 | | throw new UnauthorizedAccessException("Invalid username or password"); |
| | 1 | 18 | | var accessToken = accessTokenService.GenerateAccessToken(user); |
| | 1 | 19 | | var refreshToken = await accessTokenService.GenerateRefreshTokenAsync(user); |
| | 1 | 20 | | return new TokenResponseDto |
| | 1 | 21 | | { |
| | 1 | 22 | | AccessToken = accessToken, |
| | 1 | 23 | | RefreshToken = refreshToken.Token |
| | 1 | 24 | | }; |
| | 1 | 25 | | } |
| | | 26 | | } |
| | | 27 | | } |