add create signup code
This commit is contained in:
parent
161dcf7cab
commit
fbc6f562e3
9 changed files with 105 additions and 2 deletions
|
@ -2,6 +2,7 @@ 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.Commands.Register;
|
||||
|
||||
|
@ -9,6 +10,16 @@ internal class RegisterCommandHandler(AuthContext context) : ICommandHandler<Reg
|
|||
{
|
||||
public async Task<RegisterResult> Handle(RegisterCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var code = await context.SignupCodes
|
||||
.Where(c => c.Code == request.SignupCode)
|
||||
.Where(c => c.ExpiresAt == null || c.ExpiresAt > now)
|
||||
.Where(c => c.RedeemingUserId == null)
|
||||
.SingleOrDefaultAsync(cancellationToken);
|
||||
|
||||
if (code is null)
|
||||
throw new DomainError("invalid signup code");
|
||||
|
||||
var user = new UserIdentity(request.Username);
|
||||
|
||||
user.SetPassword(request.Password);
|
||||
|
@ -17,6 +28,8 @@ internal class RegisterCommandHandler(AuthContext context) : ICommandHandler<Reg
|
|||
|
||||
await context.AddAsync(user, cancellationToken);
|
||||
|
||||
code.Redeem(user.Id);
|
||||
|
||||
return new(new Session(session.Id, session.Expires), user.Id, user.Username);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue