fix: handle username conflict during user creation

This commit is contained in:
2026-06-13 20:15:41 -04:00
parent 18c20cdf64
commit f5a7aabf42
@@ -61,7 +61,19 @@ namespace SharpbinV3.Server.Controllers
if (!string.IsNullOrEmpty(request.DisplayName) && request.DisplayName.Length > 26) if (!string.IsNullOrEmpty(request.DisplayName) && request.DisplayName.Length > 26)
return BadRequest(new { success = false, message = "Display name should not exceed 26 characters." }); return BadRequest(new { success = false, message = "Display name should not exceed 26 characters." });
var user = await _authService.CreateUser(request.Username, request.Password, request.Email, request.DisplayName); User user;
try
{
user = await _authService.CreateUser(request.Username, request.Password, request.Email, request.DisplayName);
}
catch (DbUpdateException)
{
if (await _userService.GetByUsername(request.Username) != null)
return Conflict(new { success = false, message = "Username already exists." });
throw;
}
if (!string.IsNullOrWhiteSpace(user.Email)) if (!string.IsNullOrWhiteSpace(user.Email))
await _authService.SendEmailVerification(user); await _authService.SendEmailVerification(user);