feat: add user model and registration endpoint

This commit is contained in:
Indiana
2026-07-20 15:14:31 +00:00
parent 9e69c622c9
commit cfffc7ff59
9 changed files with 147 additions and 7 deletions

View File

@@ -0,0 +1,27 @@
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.db import get_db
from app.models.user import User
from app.schemas import RegisterRequest, UserOut
from app.security import hash_password
router = APIRouter(prefix="/auth", tags=["auth"])
@router.post("/register", response_model=UserOut, status_code=status.HTTP_201_CREATED)
async def register(payload: RegisterRequest, db: AsyncSession = Depends(get_db)):
existing = await db.scalar(select(User).where(User.username == payload.username))
if existing is not None:
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="username taken")
user = User(
username=payload.username,
password_hash=hash_password(payload.password),
email=payload.email,
)
db.add(user)
await db.commit()
await db.refresh(user)
return user