MyBookmark/Modules.Account.Application/Queries/GetAccountQuery.cs
2024-10-21 01:52:41 +03:00

24 lines
976 B
C#

using MediatR;
using Modules.Account.Application.Gateways;
using Modules.Account.Domain.Gateways;
namespace Modules.Account.Application.Queries;
public class GetAccountQuery : IRequest<Models.Account>
{
public string Email { get; set; } = default!;
public string Password { get; set; } = default!;
}
public class GetAccountQueryHandler(IAccountGateway accountGateway, IPasswordHasher passwordHasher) : IRequestHandler<GetAccountQuery, Models.Account>
{
const string error = "Invalid email or password";
public async Task<Models.Account> Handle(GetAccountQuery request, CancellationToken cancellationToken)
{
var account = await accountGateway.TryGetByEmail(request.Email);
if (account == null) throw new Exception(error);
if (passwordHasher.VerifyPassword(new Domain.Entities.Account(account.Id, account.Email, account.HashedPassword), request.Password))
throw new Exception(error);
return account;
}
}