2a9ebe24: Implement auth endpoints (register/login/JWT) with SQLAlchemy
This commit is contained in:
4
app/routers/__init__.py
Normal file
4
app/routers/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
"""Routers package."""
|
||||
from app.routers.auth import router as auth_router
|
||||
|
||||
__all__ = ["auth_router"]
|
||||
81
app/routers/auth.py
Normal file
81
app/routers/auth.py
Normal file
@@ -0,0 +1,81 @@
|
||||
"""Authentication routes for SocialPhoto API."""
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.database import get_db
|
||||
from app.schemas.auth import Token, UserLogin, UserRegister
|
||||
from app.services.auth_service import AuthService, create_access_token
|
||||
|
||||
|
||||
router = APIRouter(prefix="/auth", tags=["Authentication"])
|
||||
|
||||
|
||||
@router.post("/register", response_model=Token, status_code=status.HTTP_201_CREATED)
|
||||
async def register(
|
||||
user_data: UserRegister,
|
||||
db: Annotated[Session, Depends(get_db)],
|
||||
) -> Token:
|
||||
"""Register a new user.
|
||||
|
||||
Args:
|
||||
user_data: User registration data (username, email, password).
|
||||
db: Database session.
|
||||
|
||||
Returns:
|
||||
Token object with access token.
|
||||
|
||||
Raises:
|
||||
HTTPException: If username or email already exists.
|
||||
"""
|
||||
# Check if username exists
|
||||
existing_user = AuthService.get_user_by_username(db, user_data.username)
|
||||
if existing_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Username already registered",
|
||||
)
|
||||
|
||||
# Check if email exists
|
||||
existing_email = AuthService.get_user_by_email(db, user_data.email)
|
||||
if existing_email:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Email already registered",
|
||||
)
|
||||
|
||||
# Create user
|
||||
user = AuthService.create_user(db, user_data)
|
||||
|
||||
# Create access token
|
||||
access_token = create_access_token(data={"sub": str(user.id)})
|
||||
return Token(access_token=access_token)
|
||||
|
||||
|
||||
@router.post("/login", response_model=Token)
|
||||
async def login(
|
||||
user_data: UserLogin,
|
||||
db: Annotated[Session, Depends(get_db)],
|
||||
) -> Token:
|
||||
"""Login and get access token.
|
||||
|
||||
Args:
|
||||
user_data: User login data (username, password).
|
||||
db: Database session.
|
||||
|
||||
Returns:
|
||||
Token object with access token.
|
||||
|
||||
Raises:
|
||||
HTTPException: If credentials are invalid.
|
||||
"""
|
||||
user = AuthService.authenticate_user(db, user_data.username, user_data.password)
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect username or password",
|
||||
)
|
||||
|
||||
access_token = create_access_token(data={"sub": str(user.id)})
|
||||
return Token(access_token=access_token)
|
||||
Reference in New Issue
Block a user