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 required string EmailAddress { get; init; }
public required string Password { get; init; }
}
public class LoginCommandHandler : IRequestHandler<LoginCommand, string> public record LoginCommand(
{ string Email,
private readonly IApplicationDbContext _context; string Password)
private readonly IIdentityService _identityService; : IRequest<LoginResponse>;
public LoginCommandHandler(IApplicationDbContext context, IIdentityService identityService) public record LoginResponse(
{ string AccessToken,
_context = context; string RefreshToken);
_identityService = identityService;
}
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); 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);
} }