kaas
This commit is contained in:
+47
-1
@@ -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",
|
||||
}
|
||||
Reference in New Issue
Block a user