28 lines
901 B
C#
28 lines
901 B
C#
using Femto.Common.Domain;
|
|
using Femto.Modules.Auth.Application.Dto;
|
|
using Femto.Modules.Auth.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Femto.Modules.Auth.Application.Interface.Login;
|
|
|
|
internal class LoginCommandHandler(AuthContext context)
|
|
: ICommandHandler<LoginCommand, LoginResult>
|
|
{
|
|
public async Task<LoginResult> Handle(LoginCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var user = await context.Users.SingleOrDefaultAsync(
|
|
u => u.Username == request.Username,
|
|
cancellationToken
|
|
);
|
|
|
|
if (user is null)
|
|
throw new DomainError("invalid credentials");
|
|
|
|
if (!user.HasPassword(request.Password))
|
|
throw new DomainError("invalid credentials");
|
|
|
|
var session = user.StartNewSession();
|
|
|
|
return new(new Session(session.Id, session.Expires), new UserInfo(user));
|
|
}
|
|
}
|