fix(backend): Fix login by using ASP.NET Core Identity password hasher in seed data
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

- Update SeedDataExtension to use IPasswordHasher<User> for generating password hashes
- This ensures seed user passwords are compatible with ASP.NET Core Identity's PasswordHasher
- Admin credentials: admin@deutschlernen.com / Admin@123!
- Test user credentials: test@deutschlernen.com / Test@123!

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
Lasse Rune Hansen 2026-06-18 16:20:18 +02:00
parent 0733314aab
commit c76938cee9

View file

@ -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<IPasswordHasher<User>>();
// 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