Try to fix database sync with Antigravity.

This commit is contained in:
Creeper Lv
2026-06-01 06:38:20 +10:00
parent b263291dc2
commit c60540fb2b
4 changed files with 261 additions and 24 deletions
+59 -1
View File
@@ -18,7 +18,7 @@ public static class AuthEndpoints
{
var group = routes.MapGroup("/api/auth");
group.MapPost("/register", async (RegisterRequest req, ServerDbContext db) =>
group.MapPost("/register", async (RegisterRequest req, HttpContext context, ServerDbContext db, PeerCache peerCache, RsaKeyManager rsaKeyManager, IConfiguration configuration, HttpClient httpClient) =>
{
if (string.IsNullOrWhiteSpace(req.Username) || string.IsNullOrWhiteSpace(req.Password) || string.IsNullOrWhiteSpace(req.EncryptedKey))
{
@@ -41,9 +41,66 @@ public static class AuthEndpoints
db.Users.Add(user);
await db.SaveChangesAsync();
_ = Task.Run(async () =>
{
try
{
using var scope = context.RequestServices.CreateScope();
var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
await SyncEndpoints.BroadcastUserChangeAsync(user.Username, scopeDb, peerCache, rsaKeyManager, configuration, httpClient);
}
catch (Exception ex)
{
Console.WriteLine($"Error broadcasting user registration: {ex.Message}");
}
});
return Results.Ok(new { message = "Registration successful." });
});
group.MapPost("/change-password", async (ChangePasswordRequest req, HttpContext context, ServerDbContext db, CertificateManager certManager, PeerCache peerCache, RsaKeyManager rsaKeyManager, IConfiguration configuration, HttpClient httpClient) =>
{
var username = AuthHelper.GetAuthenticatedUser(context, certManager);
if (string.IsNullOrEmpty(username)) return Results.Unauthorized();
if (string.IsNullOrWhiteSpace(req.OldPassword) || string.IsNullOrWhiteSpace(req.NewPassword) || string.IsNullOrWhiteSpace(req.NewEncryptedKey))
{
return Results.BadRequest("Old password, new password, and new encrypted key are required.");
}
var user = await db.Users.FirstOrDefaultAsync(u => u.Username.ToLower() == username.ToLower());
if (user == null) return Results.NotFound("User not found.");
// Verify old password
if (!PasswordHasher.VerifyPassword(user.PasswordHash, req.OldPassword))
{
return Results.BadRequest("Incorrect old password.");
}
// Update user properties
user.PasswordHash = PasswordHasher.HashPassword(req.NewPassword);
user.EncryptedKey = req.NewEncryptedKey;
await db.SaveChangesAsync();
// Broadcast the user password change!
_ = Task.Run(async () =>
{
try
{
using var scope = context.RequestServices.CreateScope();
var scopeDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
await SyncEndpoints.BroadcastUserChangeAsync(user.Username, scopeDb, peerCache, rsaKeyManager, configuration, httpClient);
}
catch (Exception ex)
{
Console.WriteLine($"Error broadcasting password change: {ex.Message}");
}
});
return Results.Ok(new { message = "Password changed successfully." });
});
group.MapPost("/login", async (LoginRequest req, ServerDbContext db, CertificateManager certManager) =>
{
if (string.IsNullOrWhiteSpace(req.Username) || string.IsNullOrWhiteSpace(req.Password))
@@ -97,3 +154,4 @@ public static class AuthEndpoints
public record RegisterRequest(string Username, string Password, string EncryptedKey);
public record LoginRequest(string Username, string Password);
public record LoginResponse(string Token, string EncryptedKey);
public record ChangePasswordRequest(string OldPassword, string NewPassword, string NewEncryptedKey);