This commit is contained in:
ben
2026-08-18 14:27:52 +02:00
parent edada21b4d
commit 54581e2e42
3 changed files with 114 additions and 321 deletions
+47 -1
View File
@@ -104,4 +104,50 @@ def get_user(user_id):
WHERE id = ?
""",
(user_id,)
)
)
def change_password(user_id: int, old_password: str, new_password: str):
user = fetch_one(
"""
SELECT password_hash
FROM users
WHERE id = ?
""",
(user_id,),
)
if user is None:
return {
"status": "failed",
"reason": "user_not_found",
}
if not bcrypt.checkpw(
old_password.encode(),
user["password_hash"].encode(),
):
return {
"status": "failed",
"reason": "incorrect_password",
}
password_hash = bcrypt.hashpw(
new_password.encode(),
bcrypt.gensalt(),
).decode()
execute(
"""
UPDATE users
SET password_hash = ?
WHERE id = ?
""",
(
password_hash,
user_id,
),
)
return {
"status": "updated",
}