usermanagement / auth.py
triflix's picture
Upload 20 files
437681a verified
raw
history blame contribute delete
915 Bytes
from passlib.context import CryptContext
from sqlalchemy.orm import Session
from models import User
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
def verify_password(plain_password, hashed_password):
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password):
return pwd_context.hash(password)
def authenticate_user(db: Session, username: str, password: str):
user = db.query(User).filter(User.username == username).first()
if not user:
return None
if not verify_password(password, user.password):
return None
return user
def create_user(db: Session, username: str, password: str, is_admin: bool = False):
hashed_password = get_password_hash(password)
db_user = User(username=username, password=hashed_password, is_admin=is_admin)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user