39 lines
717 B
Python
39 lines
717 B
Python
from database import execute
|
|
import bcrypt
|
|
|
|
def create_user(username, password):
|
|
|
|
password_hash = bcrypt.hashpw(
|
|
password.encode(),
|
|
bcrypt.gensalt()
|
|
).decode()
|
|
|
|
user_id = execute(
|
|
"""
|
|
INSERT INTO users (username, password_hash)
|
|
VALUES (?, ?)
|
|
""",
|
|
(username, password_hash)
|
|
)
|
|
|
|
return user_id
|
|
|
|
def verify_user(username, password):
|
|
from database import fetch_one
|
|
|
|
user = fetch_one(
|
|
"SELECT * FROM users WHERE username = ?",
|
|
(username,)
|
|
)
|
|
|
|
if not user:
|
|
return None
|
|
|
|
if bcrypt.checkpw(
|
|
password.encode(),
|
|
user["password_hash"].encode()
|
|
):
|
|
return user["id"]
|
|
|
|
|
|
return None |