Files

39 lines
1.4 KiB
C#
Raw Normal View History

2026-01-13 00:18:29 +01:00
using MediatR;
using TimetableDesigner.Backend.Services.Authentication.Core.Helpers;
2026-01-13 00:18:29 +01:00
using TimetableDesigner.Backend.Services.Authentication.Database;
2026-01-14 01:37:46 +01:00
using TimetableDesigner.Backend.Services.Authentication.Database.Model;
2026-01-11 02:30:08 +01:00
namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.Register;
2026-01-11 02:30:08 +01:00
2026-01-14 01:37:46 +01:00
public class RegisterHandler : IRequestHandler<RegisterCommand, RegisterResult>
2026-01-11 02:30:08 +01:00
{
private readonly DatabaseContext _databaseContext;
2026-01-13 02:47:34 +01:00
private readonly IPasswordHasher _passwordHasher;
2026-01-11 02:30:08 +01:00
public RegisterHandler(DatabaseContext databaseContext, IPasswordHasher passwordHasher)
2026-01-11 02:30:08 +01:00
{
_databaseContext = databaseContext;
2026-01-13 02:47:34 +01:00
_passwordHasher = passwordHasher;
2026-01-11 02:30:08 +01:00
}
2026-01-14 01:37:46 +01:00
public async Task<RegisterResult> Handle(RegisterCommand command, CancellationToken cancellationToken)
2026-01-11 02:30:08 +01:00
{
2026-01-13 02:47:34 +01:00
PasswordHashData hash = _passwordHasher.CreateHash(command.Password);
2026-01-13 00:18:29 +01:00
2026-01-14 01:37:46 +01:00
Account account = new Account
{
Email = command.Email,
Password = hash.Hash,
PasswordSalt = hash.Salt,
};
await _databaseContext.Accounts.AddAsync(account, cancellationToken);
// Change to outbox pattern
//RegisterEvent eventData = account.ToEvent();
//await _eventQueuePublisher.PublishAsync(eventData);
await _databaseContext.SaveChangesAsync(cancellationToken);
2026-01-14 01:37:46 +01:00
return new RegisterResult(account.Id, account.Email);
2026-01-11 02:30:08 +01:00
}
}