using LiteDB; namespace LehrerApp.Api; public class UserStore(string dataPath) : IDisposable { private readonly LiteDatabase _db = new(Path.Combine(dataPath, "users.db")); private ILiteCollection Col { get { var col = _db.GetCollection("users"); col.EnsureIndex(x => x.Username, unique: true); return col; } } public bool CreateUser(string username, string password) { if (Col.Exists(x => x.Username == username)) return false; Col.Insert(new UserEntry { Username = username, PasswordHash = PasswordHasher.Hash(password) }); return true; } public bool VerifyPassword(string username, string password) { var user = Col.FindOne(x => x.Username == username); return user is not null && PasswordHasher.Verify(password, user.PasswordHash); } public void Dispose() => _db.Dispose(); } internal class UserEntry { public ObjectId Id { get; set; } = ObjectId.NewObjectId(); public string Username { get; set; } = ""; public string PasswordHash { get; set; } = ""; }