From 7bc05595e8a84b76dded4fcca698dedb384c9ca7 Mon Sep 17 00:00:00 2001 From: Hamza Alqurneh Date: Thu, 27 Aug 2026 12:57:10 +0300 Subject: [PATCH] feat: ensure default admin account exists on every startup Resets admin@bitween.systems to the default password (case-insensitive lookup, normalized lowercase email) every boot, since the EF HasData seed only ever applies once per database. --- SW.Bitween.Web/AdminAccountSeeder.cs | 35 ++++++++++++++++++++++++++++ SW.Bitween.Web/Program.cs | 1 + 2 files changed, 36 insertions(+) create mode 100644 SW.Bitween.Web/AdminAccountSeeder.cs diff --git a/SW.Bitween.Web/AdminAccountSeeder.cs b/SW.Bitween.Web/AdminAccountSeeder.cs new file mode 100644 index 0000000..4f749df --- /dev/null +++ b/SW.Bitween.Web/AdminAccountSeeder.cs @@ -0,0 +1,35 @@ +using System; +using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using SW.Bitween.Domain.Accounts; + +namespace SW.Bitween.Web +{ + public static class AdminAccountSeeder + { + private const string DefaultAdminEmail = "admin@bitween.systems"; + private const string DefaultAdminPassword = "Mtm@dmin!2"; + + public static void EnsureDefaultAdmin(IHost host) + { + using var scope = host.Services.CreateScope(); + var dbContext = scope.ServiceProvider.GetRequiredService(); + + var passwordHash = SecurePasswordHasher.Hash(DefaultAdminPassword); + var admin = dbContext.Set().SingleOrDefault(a => a.Email.ToLower() == DefaultAdminEmail); + + if (admin is null) + { + admin = new Account("Admin", DefaultAdminEmail, passwordHash, AccountRole.Admin) + { + CreatedOn = DateTime.UtcNow + }; + dbContext.Add(admin); + } + + admin.AddEmailLoginMethod(DefaultAdminEmail, passwordHash); + dbContext.SaveChanges(); + } + } +} diff --git a/SW.Bitween.Web/Program.cs b/SW.Bitween.Web/Program.cs index 5d99388..1f6554d 100644 --- a/SW.Bitween.Web/Program.cs +++ b/SW.Bitween.Web/Program.cs @@ -41,6 +41,7 @@ public static void Main(string[] args) throw; } + AdminAccountSeeder.EnsureDefaultAdmin(host); host.ApplyStoredSettings().Run(); }