refactor: more verbose api responses

fix: exclude .idea folder from rider ide
This commit is contained in:
2026-01-09 16:09:23 -05:00
parent 6bea6c49be
commit 60876eb1ba
5 changed files with 96 additions and 69 deletions
+38 -28
View File
@@ -34,7 +34,7 @@ namespace SharpbinV3.Server.Controllers
public async Task<IActionResult> Register([FromBody] RegisterUserDto request)
{
if (!options.Value.Registration_Enabled)
return BadRequest(new { message = "Registration is disabled." });
return BadRequest(new { success = false, message = "Registration is disabled." });
if (!ModelState.IsValid)
return BadRequest(ModelState);
if (!await _verification.VerifyAsync(new VerificationContext
@@ -42,19 +42,19 @@ namespace SharpbinV3.Server.Controllers
Token = request.Token,
Ip = HttpContext.GetRequestIP()
}))
return BadRequest(new { message = "Verification failed." });
return BadRequest(new { success = false, message = "Verification failed." });
var existingUser = await _userService.GetByUsername(request.Username);
if (existingUser != null)
return Conflict(new { message = "Username already exists." });
return Conflict(new { success = false, message = "Username already exists." });
if (!string.IsNullOrEmpty(request.Email))
{
var existingEmailUser = await _userService.GetByEmail(request.Email);
if (existingEmailUser != null)
return Conflict(new { message = "Email already in use." });
return Conflict(new { success = false, message = "Email already in use." });
}
if (!string.IsNullOrEmpty(request.DisplayName) && request.DisplayName.Length > 26)
return BadRequest(new { 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);
return Ok(new
@@ -79,41 +79,40 @@ namespace SharpbinV3.Server.Controllers
public async Task<IActionResult> Login([FromBody] LoginUserDto request)
{
if (string.IsNullOrEmpty(request.Username) && string.IsNullOrEmpty(request.Email))
return BadRequest(new { message = "Username or email is required." });
return BadRequest(new { success = false, message = "Username or email is required." });
if (!await _verification.VerifyAsync(new VerificationContext
{
Token = request.Token,
Ip = HttpContext.GetRequestIP()
}))
return BadRequest(new { message = "Verification failed." });
return BadRequest(new { success = false, message = "Verification failed." });
User? user = null;
if (!string.IsNullOrEmpty(request.Username))
user = await _userService.GetByUsername(request.Username);
if (user == null && !string.IsNullOrEmpty(request.Email))
user = await _userService.GetByEmail(request.Email);
if (user == null) return BadRequest(new { message = "Invalid username/email or password." });
if (!Bcrypt.Verify(request.Password, user.PasswordHash))
return BadRequest(new { message = "Invalid username/email or password." });
if (user == null || !Bcrypt.Verify(request.Password, user.PasswordHash))
return BadRequest(new { success = false, message = "Invalid username/email or password." });
var totpEnabled = await _db.UserTotps.AnyAsync(t => t.UserUUID == user.UUID);
if (totpEnabled)
{
if (string.IsNullOrWhiteSpace(request.TotpCode))
return BadRequest(new { message = "TOTP code is required." });
return BadRequest(new { success = false, message = "TOTP code is required." });
var totpValid = await _totp.VerifyAsync(new VerificationContext
{
UserUUID = user.UUID,
Code = request.TotpCode
});
if (!totpValid)
return BadRequest(new { message = "Invalid TOTP code." });
return BadRequest(new { success = false, message = "Invalid TOTP code." });
}
var token = _authService.GenerateJWTToken(user);
await _authService.UpdateLoginTime(user);
return Ok(new { token.Result });
return Ok(new { success = true, token.Result });
}
[HttpPost]
[Route("refresh")]
@@ -123,17 +122,26 @@ namespace SharpbinV3.Server.Controllers
try
{
var jwtResult = await _authService.RefreshJWTToken(request.Token, request.RefreshToken);
return Ok(new { token = jwtResult });
return Ok(new
{
success = jwtResult?.Success,
message = jwtResult?.Errors != null && jwtResult.Errors.Count > 0 ? string.Join("; ", jwtResult.Errors) : "Token refreshed successfully.",
token = new
{
jwtResult?.Token,
jwtResult?.RefreshToken
}
});
}
catch (Exception ex)
{
return BadRequest(new { message = ex.Message });
return BadRequest(new { success = false, message = ex.Message });
}
}
[HttpGet]
[Route("info")]
public async Task<IActionResult> GetSiteInfo()
public IActionResult GetSiteInfo()
{
var authSettings = options.Value;
return Ok(new
@@ -149,13 +157,13 @@ namespace SharpbinV3.Server.Controllers
public async Task<IActionResult> StartTotpEnrollment()
{
var user = await _authService.GetUserFromHttpContext(HttpContext);
if (user is null) return BadRequest(new { message = "User not found." });
if (user is null) return BadRequest(new { success = false, message = "User not found." });
var secret = _totp.GenerateSecret();
var secretBase32 = _totp.ToBase32(secret);
var account = string.IsNullOrWhiteSpace(user.Email) ? user.Username : user.Email!;
var uri = _totp.BuildOtpAuthUri("SharpbinV3", account, secretBase32);
return Ok(new { secret = secretBase32, otpauth = uri });
return Ok(new { success = true, secret = secretBase32, otpauth = uri });
}
[HttpPost]
@@ -164,13 +172,15 @@ namespace SharpbinV3.Server.Controllers
public async Task<IActionResult> EnableTotp([FromBody] EnableTotpDto request)
{
var uuidClaim = User.Claims.FirstOrDefault(c => c.Type == "UUID")?.Value;
if (string.IsNullOrEmpty(uuidClaim)) return Unauthorized();
if (string.IsNullOrEmpty(uuidClaim))
return Unauthorized(new { success = false, message = "Invalid token." });
var uuid = Guid.Parse(uuidClaim);
if (string.IsNullOrWhiteSpace(request.Secret) || string.IsNullOrWhiteSpace(request.Code))
return BadRequest(new { message = "Secret and code are required." });
return BadRequest(new { success = false, message = "Secret and code are required." });
if (!_totp.VerifyWithSecretBase32(request.Secret, request.Code))
return BadRequest(new { message = "Invalid TOTP code." });
return BadRequest(new { success = false, message = "Invalid TOTP code." });
var secretBytes = _totp.FromBase32(request.Secret);
var protectedBytes = _totp.Protect(secretBytes);
@@ -186,7 +196,7 @@ namespace SharpbinV3.Server.Controllers
_db.UserTotps.Update(existing);
}
await _db.SaveChangesAsync();
return Ok(new { message = "TOTP enabled." });
return Ok(new { success = true, message = "TOTP enabled." });
}
[HttpPost]
@@ -195,22 +205,22 @@ namespace SharpbinV3.Server.Controllers
public async Task<IActionResult> DisableTotp([FromBody] DisableTotpDto dto)
{
var user = await _authService.GetUserFromHttpContext(HttpContext);
if (user == null) return BadRequest(new { message = "User not found." });
if (user == null) return BadRequest(new { success = false, message = "User not found." });
if (string.IsNullOrWhiteSpace(dto.Code))
return BadRequest(new { message = "TOTP code is required." });
return BadRequest(new { success = false, message = "TOTP code is required." });
var existing = await _db.UserTotps.FirstOrDefaultAsync(t => t.UserUUID == user.UUID);
if (existing is null)
return BadRequest(new { message = "TOTP is not enabled." });
return BadRequest(new { success = false, message = "TOTP is not enabled." });
if (!_totp.VerifyAsync(new VerificationContext
{
UserUUID = user.UUID,
Code = dto.Code
}).Result)
return BadRequest(new { message = "Invalid TOTP code." });
return BadRequest(new { success = false, message = "Invalid TOTP code." });
_db.UserTotps.Remove(existing);
await _db.SaveChangesAsync();
return Ok(new { message = "TOTP disabled." });
return Ok(new { success = true, message = "TOTP disabled." });
}
}
}