Spaces:
Paused
Paused
Download auth.py from triflix/usermanagement: direct link, hf CLI and curl.
- Browser
- Download file 915 Bytes
-
https://huggingface.co/spaces/triflix/usermanagement/resolve/main/auth.py
- Command line
-
hf download hf://spaces/triflix/usermanagement/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/triflix/usermanagement/resolve/main/auth.py
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 | |