Refactor user management: Extract IUserService and UserService, consolidate user registration, authentication, and profile management. Fix memory leaks in ApiClient, enhance connection management, and improve error handling in AuthController and UsersController. Update IRC command handling to utilize IUserService for user operations.

This commit is contained in:
HueByte
2026-02-23 14:51:24 +01:00
parent 27a25b1b43
commit bdcff74ad5
14 changed files with 409 additions and 282 deletions
+21 -18
View File
@@ -12,6 +12,7 @@ public sealed class IrcCommandHandler
private readonly IrcClientConnection _conn;
private readonly IrcOptions _options;
private readonly IChatService _chatService;
private readonly IUserService _userService;
private readonly IChannelService _channelService;
private readonly IMessageEncryptionService _encryption;
private readonly ILogger _logger;
@@ -22,6 +23,7 @@ public sealed class IrcCommandHandler
IrcClientConnection conn,
IrcOptions options,
IChatService chatService,
IUserService userService,
IChannelService channelService,
IMessageEncryptionService encryption,
ILogger logger)
@@ -29,6 +31,7 @@ public sealed class IrcCommandHandler
_conn = conn;
_options = options;
_chatService = chatService;
_userService = userService;
_channelService = channelService;
_encryption = encryption;
_logger = logger;
@@ -168,23 +171,23 @@ public sealed class IrcCommandHandler
_logger.LogDebug("SASL PLAIN auth attempt for user '{Username}' (connection {Id})",
username, _conn.ConnectionId);
var result = await _chatService.AuthenticateUserAsync(username, password);
var result = await _userService.AuthenticateUserAsync(username, password);
// Auth failed — try registering a new account
if (result is null)
result = await _chatService.RegisterUserAsync(username, password);
if (!result.IsSuccess)
result = await _userService.RegisterUserAsync(username, password);
if (result is null)
if (!result.IsSuccess)
{
_logger.LogWarning("SASL auth/register failed for user '{Username}' (connection {Id})",
username, _conn.ConnectionId);
_logger.LogWarning("SASL auth/register failed for user '{Username}': {Error} (connection {Id})",
username, result.ErrorMessage, _conn.ConnectionId);
await _conn.SendNumericAsync(ServerName, IrcNumericReply.ERR_SASLFAIL,
":SASL authentication failed");
$":SASL authentication failed — {result.ErrorMessage}");
return;
}
_conn.Nickname = result.Value.Username;
_conn.UserId = result.Value.UserId;
_conn.Nickname = result.User!.Username;
_conn.UserId = result.User!.Id;
_conn.IsAuthenticated = true;
_logger.LogInformation("SASL auth succeeded for user '{Username}' (connection {Id})",
@@ -288,26 +291,26 @@ public sealed class IrcCommandHandler
return;
}
var result = await _chatService.AuthenticateUserAsync(_conn.Nickname!, _conn.Password);
var result = await _userService.AuthenticateUserAsync(_conn.Nickname!, _conn.Password);
// Auth failed — try registering a new account
if (result is null)
result = await _chatService.RegisterUserAsync(_conn.Nickname!, _conn.Password);
if (!result.IsSuccess)
result = await _userService.RegisterUserAsync(_conn.Nickname!, _conn.Password);
if (result is null)
if (!result.IsSuccess)
{
await _conn.SendNumericAsync(ServerName, IrcNumericReply.ERR_PASSWDMISMATCH,
":Password incorrect.");
$":{result.ErrorMessage}");
await _conn.SendAsync("ERROR :Authentication failed");
return;
}
_conn.UserId = result.Value.UserId;
_conn.Nickname = result.Value.Username;
_conn.UserId = result.User!.Id;
_conn.Nickname = result.User!.Username;
_conn.IsAuthenticated = true;
_conn.IsRegistered = true;
await _chatService.UserConnectedAsync(_conn.ConnectionId, result.Value.UserId, result.Value.Username);
await _chatService.UserConnectedAsync(_conn.ConnectionId, result.User!.Id, result.User!.Username);
await SendWelcomeBurstAsync();
}
@@ -559,7 +562,7 @@ public sealed class IrcCommandHandler
if (msg.Parameters.Count < 1) return;
var nick = msg.Parameters[^1].ToLowerInvariant();
var profile = await _chatService.GetUserProfileAsync(nick);
var profile = await _userService.GetUserProfileAsync(nick);
if (profile is null)
{