add create signup code

This commit is contained in:
john 2025-05-18 19:11:43 +02:00
parent 161dcf7cab
commit fbc6f562e3
9 changed files with 105 additions and 2 deletions

View file

@ -0,0 +1,5 @@
using Femto.Common.Domain;
namespace Femto.Modules.Auth.Application.Commands.CreateSignupCode;
public record CreateSignupCodeCommand(string Code, string RecipientEmail, string RecipientName): ICommand;

View file

@ -0,0 +1,15 @@
using Femto.Common.Domain;
using Femto.Modules.Auth.Data;
using Femto.Modules.Auth.Models;
namespace Femto.Modules.Auth.Application.Commands.CreateSignupCode;
internal class CreateSignupCodeCommandHandler(AuthContext context) : ICommandHandler<CreateSignupCodeCommand>
{
public async Task Handle(CreateSignupCodeCommand command, CancellationToken cancellationToken)
{
var code = new SignupCode(command.RecipientEmail, command.RecipientName, command.Code);
await context.SignupCodes.AddAsync(code, cancellationToken);
}
}

View file

@ -3,4 +3,4 @@ using Femto.Modules.Auth.Application.Dto;
namespace Femto.Modules.Auth.Application.Commands.Register;
public record RegisterCommand(string Username, string Password) : ICommand<RegisterResult>;
public record RegisterCommand(string Username, string Password, string SignupCode) : ICommand<RegisterResult>;

View file

@ -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);
}
}