mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-05 18:48:41 +00:00
refactor(api): migrate account deletion from minimal API to controller
This commit is contained in:
@@ -253,6 +253,33 @@ namespace SharpbinV2.Server.Controllers
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete("delete")]
|
||||
public async Task<IActionResult> DeleteAccount()
|
||||
{
|
||||
try
|
||||
{
|
||||
var requestDetails = HelperService.GetRequestDetails(HttpContext);
|
||||
if (string.IsNullOrWhiteSpace(requestDetails.Token))
|
||||
{
|
||||
return Unauthorized(new { success = false, message = "Not authenticated" });
|
||||
}
|
||||
var user = await _databaseService.UserFromToken(requestDetails.Token);
|
||||
if (user == null)
|
||||
{
|
||||
return Unauthorized(new { success = false, message = "Not authenticated" });
|
||||
}
|
||||
await _databaseService.DeleteUser(user);
|
||||
await _databaseService.DeleteSession(requestDetails.Token);
|
||||
Response.Cookies.Delete("Authorization");
|
||||
return Ok(new { success = true, message = "Account deleted successfully" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during account deletion attempt");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
}
|
||||
public class AuthRequest
|
||||
{
|
||||
|
||||
@@ -314,74 +314,6 @@ namespace SharpbinV2.Server
|
||||
|
||||
#region API
|
||||
|
||||
app.MapPost("/api/accounts/delete", async (HttpContext context) =>
|
||||
{
|
||||
var requestdetails = GetRequestDetails(context);
|
||||
if (string.IsNullOrEmpty(requestdetails.Token))
|
||||
{
|
||||
context.Response.StatusCode = 400;
|
||||
await context.Response.WriteAsJsonAsync(new { success = false, message = "No token provided, not authorized." });
|
||||
return;
|
||||
}
|
||||
var user = await Database.UserFromToken(requestdetails.Token);
|
||||
if (user == null)
|
||||
{
|
||||
context.Response.StatusCode = 400;
|
||||
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid token." });
|
||||
return;
|
||||
}
|
||||
using (var connection = new SqliteConnection(MainDatabaseConnection))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
using (var command = connection.CreateCommand())
|
||||
{
|
||||
command.CommandText = "DELETE FROM users WHERE UUID = @UUID;";
|
||||
command.Parameters.AddWithValue("@UUID", user.UUID);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
await connection.CloseAsync();
|
||||
}
|
||||
|
||||
using (var connection = new SqliteConnection(MainDatabaseConnection))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
using (var command = connection.CreateCommand())
|
||||
{
|
||||
command.CommandText = "DELETE FROM pastes WHERE AuthorUUID = @AuthorUUID;";
|
||||
command.Parameters.AddWithValue("@AuthorUUID", user.UUID);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
await connection.CloseAsync();
|
||||
}
|
||||
|
||||
using (var connection = new SqliteConnection(MainDatabaseConnection))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
using (var command = connection.CreateCommand())
|
||||
{
|
||||
command.CommandText = "DELETE FROM sessions WHERE UserUUID = @UserUUID;";
|
||||
command.Parameters.AddWithValue("@UserUUID", user.UUID);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
await connection.CloseAsync();
|
||||
}
|
||||
|
||||
using (var connection = new SqliteConnection(MainDatabaseConnection))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
using (var command = connection.CreateCommand())
|
||||
{
|
||||
command.CommandText = "UPDATE views SET UserUUID = @UserUUID WHERE UserUUID = @OldUserUUID;";
|
||||
command.Parameters.AddWithValue("@UserUUID", "0");
|
||||
command.Parameters.AddWithValue("@OldUserUUID", user.UUID);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
await connection.CloseAsync();
|
||||
}
|
||||
context.Response.StatusCode = 200;
|
||||
await context.Response.WriteAsJsonAsync(new { success = true, message = "Account deleted." });
|
||||
|
||||
}).RequireRateLimiting("auth");
|
||||
app.MapPost("/api/accounts/reset-password", async (HttpContext context) =>
|
||||
{
|
||||
var requestdetails = GetRequestDetails(context);
|
||||
|
||||
@@ -459,5 +459,41 @@ namespace SharpbinV2.Server.Services
|
||||
}
|
||||
return user;
|
||||
}
|
||||
public async Task<bool> DeleteUser(User user)
|
||||
{
|
||||
if (user == null)
|
||||
return false;
|
||||
using (var connection = new SqliteConnection(_connectionString))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
using (var command = connection.CreateCommand())
|
||||
{
|
||||
command.CommandText = "DELETE FROM users WHERE UUID = @UUID;";
|
||||
command.Parameters.AddWithValue("@UUID", user.UUID);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
using (var command = connection.CreateCommand())
|
||||
{
|
||||
command.CommandText = "DELETE FROM pastes WHERE AuthorUUID = @AuthorUUID;";
|
||||
command.Parameters.AddWithValue("@AuthorUUID", user.UUID);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
using (var command = connection.CreateCommand())
|
||||
{
|
||||
command.CommandText = "DELETE FROM sessions WHERE UserUUID = @UserUUID;";
|
||||
command.Parameters.AddWithValue("@UserUUID", user.UUID);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
// anonymize all views by user
|
||||
using (var command = connection.CreateCommand())
|
||||
{
|
||||
command.CommandText = "UPDATE views SET UserUUID = @UserUUID WHERE UserUUID = @OldUserUUID;";
|
||||
command.Parameters.AddWithValue("@UserUUID", "0");
|
||||
command.Parameters.AddWithValue("@OldUserUUID", user.UUID);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,7 +264,7 @@ function deleteAccount() {
|
||||
confirmBtn.onclick = async function () {
|
||||
if (confirmBtn.disabled) return;
|
||||
try {
|
||||
await fetch('/api/accounts/delete', { method: 'POST'});
|
||||
await fetch('/api/auth/delete', { method: 'DELETE'});
|
||||
} catch { }
|
||||
window.location.href = '/register';
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user