session
This commit is contained in:
parent
baea64229b
commit
0dc41337da
36 changed files with 324 additions and 95 deletions
|
@ -0,0 +1,33 @@
|
|||
using Femto.Common.Domain;
|
||||
using Femto.Modules.Auth.Application.Dto;
|
||||
using Femto.Modules.Auth.Application.Services;
|
||||
using Femto.Modules.Auth.Data;
|
||||
using Femto.Modules.Auth.Models;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Femto.Modules.Auth.Application.Commands.Login;
|
||||
|
||||
internal class LoginCommandHandler(AuthContext context, SessionGenerator sessionGenerator)
|
||||
: 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 DomainException("invalid credentials");
|
||||
|
||||
if (!user.HasPassword(request.Password))
|
||||
throw new DomainException("invalid credentials");
|
||||
|
||||
var session = sessionGenerator.GenerateSession();
|
||||
|
||||
await context.AddAsync(session, cancellationToken);
|
||||
|
||||
return new(new Session(session.Id, session.Expires), user.Id, user.Username);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue