| | | 1 | | using MechanicsSoftware.Application.Abstractions; |
| | | 2 | | using MechanicsSoftware.Application.Exceptions; |
| | | 3 | | using MechanicsSoftware.Application.UseCases.Auth.Commands; |
| | | 4 | | using MechanicsSoftware.Domain.ValueObjects; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | |
| | | 7 | | namespace MechanicsSoftware.Application.UseCases.Auth.Handlers; |
| | | 8 | | |
| | | 9 | | public sealed record LoginResponse(string Token, DateTime ExpiresAt); |
| | | 10 | | |
| | 10 | 11 | | public sealed class LoginHandler(IAppDbContext context, IPasswordHasher hasher, IJwtProvider jwt) |
| | | 12 | | { |
| | | 13 | | public async Task<LoginResponse> ExecuteAsync(LoginCommand command, CancellationToken cancellationToken = default) |
| | 10 | 14 | | { |
| | 10 | 15 | | var emailVo = new Email(command.Email); |
| | 10 | 16 | | var user = await context.Users |
| | 10 | 17 | | .FirstOrDefaultAsync(u => u.Email == emailVo, cancellationToken) |
| | 10 | 18 | | ?? throw new NotFoundException("User", command.Email); |
| | | 19 | | |
| | 8 | 20 | | if (!hasher.Verify(command.Password, user.PasswordHash)) |
| | 4 | 21 | | throw new UnauthorizedException(); |
| | | 22 | | |
| | 4 | 23 | | var token = jwt.Generate(user); |
| | 4 | 24 | | return new LoginResponse(token.Token, token.ExpiresAt); |
| | 4 | 25 | | } |
| | | 26 | | } |