Add initial project

This commit is contained in:
2026-07-30 23:25:20 -04:00
commit cff6aa8289
164 changed files with 26672 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
"""Authentication service for business logic."""
from typing import Optional
from sqlalchemy.orm import Session
from backend.models.user import User
from backend import auth
from backend.schemas.user import UserCreate
class AuthService:
"""Service for authentication operations."""
@staticmethod
def register_user(db: Session, user_data: UserCreate) -> User:
"""Register a new user."""
db_user = db.query(User).filter(
User.username == user_data.username
).first()
if db_user:
raise ValueError("Username already registered")
hashed_password = auth.get_password_hash(user_data.password)
new_user = models.User(
username=user_data.username,
hashed_password=hashed_password
)
db.add(new_user)
db.commit()
db.refresh(new_user)
return new_user
@staticmethod
def authenticate_user(db: Session, username: str, password: str) -> Optional[User]:
"""Authenticate a user with username and password."""
user = db.query(User).filter(
User.username == username
).first()
if not user or not auth.verify_password(password, user.hashed_password):
return None
return user
@staticmethod
def create_access_token(username: str) -> str:
"""Create a JWT access token for a user."""
from datetime import timedelta
from backend.core.config import settings
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = auth.create_access_token(
data={"sub": username},
expires_delta=access_token_expires
)
return access_token