Download app/services/auth_user.py from binhugg2/api: direct link, hf CLI and curl.
- Browser
- Download file 971 Bytes
-
https://huggingface.co/spaces/binhugg2/api/resolve/main/app/services/auth_user.py
- Command line
-
hf download hf://spaces/binhugg2/api/app/services/auth_user.py
-
curl -L -o auth_user.py https://huggingface.co/spaces/binhugg2/api/resolve/main/app/services/auth_user.py
971 Bytes
| from sqlalchemy import select | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from app.models.user import User | |
| from app.utils.auth import hash_password, verify_password, create_access_token | |
| async def get_user_by_username(db: AsyncSession, username: str) -> User | None: | |
| result = await db.execute(select(User).where(User.username == username)) | |
| return result.scalar_one_or_none() | |
| async def get_user_by_id(db: AsyncSession, user_id: int) -> User | None: | |
| result = await db.execute(select(User).where(User.id == user_id)) | |
| return result.scalar_one_or_none() | |
| async def authenticate_user(db: AsyncSession, username: str, password: str) -> User | None: | |
| user = await get_user_by_username(db, username) | |
| if not user or not user.is_active: | |
| return None | |
| if not verify_password(password, user.password_hash): | |
| return None | |
| return user | |
| def generate_user_token(user: User) -> str: | |
| return create_access_token(str(user.id), "user") | |