diff --git a/app/core/auth.py b/app/core/auth.py index b31d2a4..8c81fe4 100644 --- a/app/core/auth.py +++ b/app/core/auth.py @@ -56,7 +56,7 @@ def decode_token(token: str) -> Optional[Dict[str, Any]]: return None def seed_admin(): - """Seed default admin account on initial launch if not exists or update password hash if outdated.""" + """Seed default admin account on initial launch only — never resets existing password.""" conn = get_db_connection() cursor = conn.cursor() @@ -64,9 +64,8 @@ def seed_admin(): hashed_pwd = hash_password(default_pwd) now = time.time() - cursor.execute("SELECT id, hashed_password, must_change_password FROM users WHERE username = ?", ("admin",)) - row = cursor.fetchone() - if not row: + cursor.execute("SELECT id FROM users WHERE username = ?", ("admin",)) + if not cursor.fetchone(): admin_id = str(uuid.uuid4()) cursor.execute(""" INSERT INTO users (id, username, email, hashed_password, role, must_change_password, created_at, is_active) @@ -78,11 +77,6 @@ def seed_admin(): VALUES (?, 10240, 64) """, (admin_id,)) conn.commit() - else: - # Kiểm tra và sửa password admin mặc định nếu cần - if not verify_password(default_pwd, row["hashed_password"]): - cursor.execute("UPDATE users SET hashed_password = ?, must_change_password = 1 WHERE id = ?", (hashed_pwd, row["id"])) - conn.commit() conn.close() diff --git a/wiki.md b/wiki.md index abc367f..18276fd 100644 --- a/wiki.md +++ b/wiki.md @@ -790,3 +790,8 @@ - **Các file ảnh hưởng:** `app/static/js/app.jsx` - **Ghi chú/Test (nếu có):** `npm run build` — build passes. --- + +### [2026-07-29 11:27] Task: Fix seed_admin() resetting admin password on every restart +- **Tóm tắt thay đổi:** Xóa `else` branch trong `seed_admin()` — từng kiểm tra `if not verify_password(default_pwd, ...)` và reset password admin về `admin123` + `must_change_password=1` mỗi lần server restart nếu password đã bị đổi. Giờ chỉ create admin khi chưa tồn tại. +- **Các file ảnh hưởng:** `app/core/auth.py` +---