Fix login
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user