Skip to content

Commit 7bc0559

Browse files
committed
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.
1 parent a615d92 commit 7bc0559

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Linq;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
using SW.Bitween.Domain.Accounts;
6+
7+
namespace SW.Bitween.Web
8+
{
9+
public static class AdminAccountSeeder
10+
{
11+
private const string DefaultAdminEmail = "admin@bitween.systems";
12+
private const string DefaultAdminPassword = "Mtm@dmin!2";
13+
14+
public static void EnsureDefaultAdmin(IHost host)
15+
{
16+
using var scope = host.Services.CreateScope();
17+
var dbContext = scope.ServiceProvider.GetRequiredService<BitweenDbContext>();
18+
19+
var passwordHash = SecurePasswordHasher.Hash(DefaultAdminPassword);
20+
var admin = dbContext.Set<Account>().SingleOrDefault(a => a.Email.ToLower() == DefaultAdminEmail);
21+
22+
if (admin is null)
23+
{
24+
admin = new Account("Admin", DefaultAdminEmail, passwordHash, AccountRole.Admin)
25+
{
26+
CreatedOn = DateTime.UtcNow
27+
};
28+
dbContext.Add(admin);
29+
}
30+
31+
admin.AddEmailLoginMethod(DefaultAdminEmail, passwordHash);
32+
dbContext.SaveChanges();
33+
}
34+
}
35+
}

SW.Bitween.Web/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static void Main(string[] args)
4141
throw;
4242
}
4343

44+
AdminAccountSeeder.EnsureDefaultAdmin(host);
4445
host.ApplyStoredSettings().Run();
4546
}
4647

0 commit comments

Comments
 (0)