DeutschLernen/GermanApp/Domain/Entities/User.cs
Lasse Rune Hansen 42778ec34b fix(backend/auth): Fix authentication bugs
- Fix case-insensitive email lookups in AuthService (RegisterAsync, LoginAsync, CreateAdminUserAsync)
- Fix User.Create factory method to explicitly set Role property (C# object initializer behavior)
- Assign Admin role to seeded admin user in SeedDataExtension
- Add [AllowAnonymous] to Register and Login endpoints in AuthController (defensive programming)
- Increase JWT ClockSkew to 5 minutes for clock difference tolerance

These fixes resolve issues where:
- Login fails with 401 due to case-sensitive email comparison
- User role is null instead of 'User' or 'Admin'
- JWT token validation too strict on clock synchronization

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-14 16:40:01 +02:00

107 lines
2.9 KiB
C#

namespace GermanApp.Domain.Entities;
/// <summary>
/// Represents a user in the DeutschLernen system.
/// </summary>
public class User
{
public int Id { get; private set; }
public string Username { get; private set; } = string.Empty;
public string Email { get; private set; } = string.Empty;
public string PasswordHash { get; private set; } = string.Empty;
public string CurrentLevel { get; private set; } = "A1";
public string Role { get; private set; } = "User"; // Default role is "User", "Admin" for administrators
public int Streak { get; private set; }
public int TotalPoints { get; private set; }
public DateTime CreatedAt { get; private set; }
/// <summary>
/// Constructor for EF Core deserialization.
/// </summary>
private User() { }
/// <summary>
/// Factory method to create a new user.
/// </summary>
public static User Create(string username, string email, string passwordHash)
{
return new User
{
Username = username,
Email = email.ToLowerInvariant(),
PasswordHash = passwordHash,
CurrentLevel = "A1",
Role = "User", // Explicitly set default role
Streak = 0,
TotalPoints = 0,
CreatedAt = DateTime.UtcNow
};
}
/// <summary>
/// Updates user's gamification stats.
/// </summary>
public void AddPoints(int points)
{
TotalPoints += points;
}
/// <summary>
/// Updates user's streak.
/// </summary>
public void UpdateStreak(int streak)
{
Streak = streak;
}
/// <summary>
/// Updates user's current level.
/// </summary>
public void UpdateLevel(string level)
{
CurrentLevel = level;
}
/// <summary>
/// Changes user's email.
/// </summary>
public void ChangeEmail(string newEmail)
{
Email = newEmail?.ToLowerInvariant() ?? string.Empty;
}
/// <summary>
/// Changes user's password hash.
/// </summary>
public void ChangePassword(string newPasswordHash)
{
PasswordHash = newPasswordHash;
}
/// <summary>
/// Assigns admin role to user (bootstrap only).
/// </summary>
public void AssignAdminRole()
{
Role = "Admin";
}
/// <summary>
/// Sets the user's role.
/// </summary>
/// <param name="role">The role to assign (User or Admin)</param>
public void SetRole(string role)
{
if (role != "Admin" && role != "User")
throw new ArgumentException("Role must be 'Admin' or 'User'.", nameof(role));
Role = role;
}
/// <summary>
/// Checks if user has admin role.
/// </summary>
public bool IsAdmin() => Role == "Admin";
// Navigation properties
public virtual ICollection<StoryProgress> StoryProgress { get; private set; } = new List<StoryProgress>();
}