session
This commit is contained in:
parent
baea64229b
commit
0dc41337da
36 changed files with 324 additions and 95 deletions
|
@ -0,0 +1,6 @@
|
|||
using Femto.Common.Domain;
|
||||
using Femto.Modules.Auth.Application.Dto;
|
||||
|
||||
namespace Femto.Modules.Auth.Application.Commands.ValidateSession;
|
||||
|
||||
public record ValidateSessionCommand(string SessionId) : ICommand<ValidateSessionResult>;
|
|
@ -0,0 +1,34 @@
|
|||
using Femto.Common.Domain;
|
||||
using Femto.Modules.Auth.Application.Dto;
|
||||
using Femto.Modules.Auth.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Femto.Modules.Auth.Application.Commands.ValidateSession;
|
||||
|
||||
internal class ValidateSessionCommandHandler(AuthContext context)
|
||||
: ICommandHandler<ValidateSessionCommand, ValidateSessionResult>
|
||||
{
|
||||
public async Task<ValidateSessionResult> Handle(
|
||||
ValidateSessionCommand request,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
var user = await context.Users.SingleOrDefaultAsync(
|
||||
u => u.Sessions.Any(s => s.Id == request.SessionId && s.Expires > now),
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
if (user is null)
|
||||
throw new DomainException("invalid session");
|
||||
|
||||
var session = user.StartNewSession();
|
||||
|
||||
return new ValidateSessionResult(
|
||||
new Session(session.Id, session.Expires),
|
||||
user.Id,
|
||||
user.Username
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue