diff --git a/GermanApp/Infrastructure/Data/SeedData/SeedDataExtension.cs b/GermanApp/Infrastructure/Data/SeedData/SeedDataExtension.cs index 8faeb58..4d73828 100644 --- a/GermanApp/Infrastructure/Data/SeedData/SeedDataExtension.cs +++ b/GermanApp/Infrastructure/Data/SeedData/SeedDataExtension.cs @@ -1,5 +1,6 @@ using GermanApp.Domain.Entities; using GermanApp.Infrastructure.Data.DbContext; +using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; namespace GermanApp.Infrastructure.Data.SeedData; @@ -24,6 +25,17 @@ public static class SeedDataExtension // Only seed if there are no users if (!dbContext.Users.Any()) { + // Get password hasher from service provider + var passwordHasher = scope.ServiceProvider.GetRequiredService>(); + + // Create temporary users for hashing (we need actual User objects) + var tempAdminUser = User.Create("admin", "admin@deutschlernen.com", ""); + var tempTestUser = User.Create("testuser", "test@deutschlernen.com", ""); + + // Hash passwords using ASP.NET Core Identity's PasswordHasher + var adminPasswordHash = passwordHasher.HashPassword(tempAdminUser, "Admin@123!"); + var testPasswordHash = passwordHasher.HashPassword(tempTestUser, "Test@123!"); + // Seed CEFR levels first var levels = new[] { @@ -36,24 +48,14 @@ public static class SeedDataExtension dbContext.Levels.AddRange(levels); await dbContext.SaveChangesAsync(); - // Seed an admin user - // Using hardcoded password hash - bcrypt hash of "Admin@123!" - // Note: This hash is compatible with ASP.NET Core Identity's PasswordHasher - var adminUser = User.Create( - "admin", - "admin@deutschlernen.com", - "$2a$11$N9qo8uLOickgx2ZMRZoMy.Mrq9Hq4yVJyX2sX7z3J0J9z6J9Z2K4a" - ); + // Seed an admin user with properly hashed password + var adminUser = User.Create("admin", "admin@deutschlernen.com", adminPasswordHash); adminUser.AssignAdminRole(); // Set role to Admin adminUser.UpdateLevel("C1"); dbContext.Users.Add(adminUser); - // Seed a test user - var testUser = User.Create( - "testuser", - "test@deutschlernen.com", - "$2a$11$N9qo8uLOickgx2ZMRZoMy.Mrq9Hq4yVJyX2sX7z3J0J9z6J9Z2K4a" // bcrypt hash of "Test@123!" - ); + // Seed a test user with properly hashed password + var testUser = User.Create("testuser", "test@deutschlernen.com", testPasswordHash); dbContext.Users.Add(testUser); // Seed some lessons