Fix login

This commit is contained in:
Jonathan Bourdon
2024-06-20 13:46:53 -04:00
parent 390bf0b72a
commit b17d2f2366
2 changed files with 19 additions and 19 deletions

View File

@@ -1,27 +1,27 @@
using Hutopy.Application.Common.Interfaces; using Hutopy.Application.Common.Interfaces;
namespace Hutopy.Application.Users.Commands; namespace Hutopy.Application.Users.Commands;
public record LoginCommand : IRequest<string>
public record LoginCommand(
string Email,
string Password)
: IRequest<LoginResponse>;
public record LoginResponse(
string AccessToken,
string RefreshToken);
public class LoginCommandHandler(
IApplicationDbContext Context,
IIdentityService identityService)
: IRequestHandler<LoginCommand, LoginResponse>
{ {
public required string EmailAddress { get; init; } public async Task<LoginResponse> Handle(LoginCommand request, CancellationToken cancellationToken)
public required string Password { get; init; }
}
public class LoginCommandHandler : IRequestHandler<LoginCommand, string>
{
private readonly IApplicationDbContext _context;
private readonly IIdentityService _identityService;
public LoginCommandHandler(IApplicationDbContext context, IIdentityService identityService)
{ {
_context = context; var accessToken = await identityService.LoginAsync(request.Email, request.Password);
_identityService = identityService;
}
public async Task<string> Handle(LoginCommand request, CancellationToken cancellationToken) if (string.IsNullOrWhiteSpace(accessToken)) throw new InvalidOperationException("Invalid login credentials");
{
var jwt = await _identityService.LoginAsync(request.EmailAddress, request.Password);
return jwt ?? "Invalid login credentials"; return new LoginResponse(accessToken, string.Empty);
} }
} }

View File

@@ -24,7 +24,7 @@ public class Users : EndpointGroupBase
return await sender.Send(query); return await sender.Send(query);
} }
private static async Task<string> Login(ISender sender, LoginCommand command) private static async Task<LoginResponse> Login(ISender sender, LoginCommand command)
{ {
return await sender.Send(command); return await sender.Send(command);
} }