31 lines
995 B
C#
31 lines
995 B
C#
using Femto.Common.Domain;
|
|
using Femto.Modules.Auth.Application.Dto;
|
|
using Femto.Modules.Auth.Data;
|
|
using Femto.Modules.Auth.Models;
|
|
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 = Session.Strong(user.Id);
|
|
|
|
await context.AddAsync(session, cancellationToken);
|
|
|
|
return new(new SessionDto(session), new UserInfo(user));
|
|
}
|
|
}
|