Fix login

This commit is contained in:
Jonathan Bourdon
2024-06-20 13:46:53 -04:00
parent 310cfdfeb9
commit 499a945036
2 changed files with 19 additions and 19 deletions

View File

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

View File

@@ -24,7 +24,7 @@ public class Users : EndpointGroupBase
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);
}