Refactor authentication and database session handling; update user schemas for enhanced functionality and compatibility with FastAPI-Users.
This commit is contained in:
parent
1c08e57afd
commit
72b988b79b
@ -4,7 +4,7 @@ from fastapi import APIRouter, Depends
|
|||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.sql import text
|
from sqlalchemy.sql import text
|
||||||
|
|
||||||
from app.database import get_db
|
from app.database import get_async_session
|
||||||
from app.schemas.health import HealthStatus
|
from app.schemas.health import HealthStatus
|
||||||
from app.core.exceptions import DatabaseConnectionError
|
from app.core.exceptions import DatabaseConnectionError
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ router = APIRouter()
|
|||||||
description="Checks the operational status of the API and its connection to the database.",
|
description="Checks the operational status of the API and its connection to the database.",
|
||||||
tags=["Health"]
|
tags=["Health"]
|
||||||
)
|
)
|
||||||
async def check_health(db: AsyncSession = Depends(get_db)):
|
async def check_health(db: AsyncSession = Depends(get_async_session)):
|
||||||
"""
|
"""
|
||||||
Health check endpoint. Verifies API reachability and database connection.
|
Health check endpoint. Verifies API reachability and database connection.
|
||||||
"""
|
"""
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from fastapi import Depends, Request
|
from fastapi import Depends, Request
|
||||||
|
from fastapi.security import OAuth2PasswordRequestForm
|
||||||
from fastapi_users import BaseUserManager, FastAPIUsers, IntegerIDMixin
|
from fastapi_users import BaseUserManager, FastAPIUsers, IntegerIDMixin
|
||||||
from fastapi_users.authentication import (
|
from fastapi_users.authentication import (
|
||||||
AuthenticationBackend,
|
AuthenticationBackend,
|
||||||
BearerTransport,
|
BearerTransport,
|
||||||
JWTStrategy,
|
JWTStrategy,
|
||||||
OAuth2PasswordRequestForm,
|
|
||||||
)
|
)
|
||||||
from fastapi_users.db import SQLAlchemyUserDatabase
|
from fastapi_users.db import SQLAlchemyUserDatabase
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
@ -82,8 +82,8 @@ Organic Bananas
|
|||||||
HEALTH_STATUS_ERROR: str = "error"
|
HEALTH_STATUS_ERROR: str = "error"
|
||||||
|
|
||||||
# --- Auth Settings --- (These are largely handled by FastAPI-Users now)
|
# --- Auth Settings --- (These are largely handled by FastAPI-Users now)
|
||||||
# OAUTH2_TOKEN_URL: str = "/api/v1/auth/login" # FastAPI-Users has its own token URL
|
OAUTH2_TOKEN_URL: str = "/api/v1/auth/login" # FastAPI-Users has its own token URL
|
||||||
# TOKEN_TYPE: str = "bearer"
|
TOKEN_TYPE: str = "bearer"
|
||||||
# AUTH_HEADER_PREFIX: str = "Bearer"
|
# AUTH_HEADER_PREFIX: str = "Bearer"
|
||||||
# AUTH_HEADER_NAME: str = "WWW-Authenticate"
|
# AUTH_HEADER_NAME: str = "WWW-Authenticate"
|
||||||
# AUTH_CREDENTIALS_ERROR: str = "Could not validate credentials"
|
# AUTH_CREDENTIALS_ERROR: str = "Could not validate credentials"
|
||||||
|
@ -30,7 +30,7 @@ AsyncSessionLocal = sessionmaker(
|
|||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
|
||||||
# Dependency to get DB session in path operations
|
# Dependency to get DB session in path operations
|
||||||
async def get_db() -> AsyncSession: # type: ignore
|
async def get_async_session() -> AsyncSession: # type: ignore
|
||||||
"""
|
"""
|
||||||
Dependency function that yields an AsyncSession.
|
Dependency function that yields an AsyncSession.
|
||||||
Ensures the session is closed after the request.
|
Ensures the session is closed after the request.
|
||||||
@ -45,3 +45,6 @@ async def get_db() -> AsyncSession: # type: ignore
|
|||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
await session.close() # Not strictly necessary with async context manager, but explicit
|
await session.close() # Not strictly necessary with async context manager, but explicit
|
||||||
|
|
||||||
|
# Alias for backward compatibility
|
||||||
|
get_db = get_async_session
|
@ -13,6 +13,7 @@ from app.core.api_config import API_METADATA, API_TAGS
|
|||||||
from app.auth import fastapi_users, auth_backend
|
from app.auth import fastapi_users, auth_backend
|
||||||
from app.models import User
|
from app.models import User
|
||||||
from app.api.auth.oauth import router as oauth_router
|
from app.api.auth.oauth import router as oauth_router
|
||||||
|
from app.schemas.user import UserPublic, UserCreate, UserUpdate
|
||||||
|
|
||||||
# Initialize Sentry
|
# Initialize Sentry
|
||||||
sentry_sdk.init(
|
sentry_sdk.init(
|
||||||
@ -77,7 +78,7 @@ app.include_router(
|
|||||||
tags=["auth"],
|
tags=["auth"],
|
||||||
)
|
)
|
||||||
app.include_router(
|
app.include_router(
|
||||||
fastapi_users.get_register_router(),
|
fastapi_users.get_register_router(UserPublic, UserCreate),
|
||||||
prefix="/auth",
|
prefix="/auth",
|
||||||
tags=["auth"],
|
tags=["auth"],
|
||||||
)
|
)
|
||||||
@ -87,12 +88,12 @@ app.include_router(
|
|||||||
tags=["auth"],
|
tags=["auth"],
|
||||||
)
|
)
|
||||||
app.include_router(
|
app.include_router(
|
||||||
fastapi_users.get_verify_router(),
|
fastapi_users.get_verify_router(UserPublic),
|
||||||
prefix="/auth",
|
prefix="/auth",
|
||||||
tags=["auth"],
|
tags=["auth"],
|
||||||
)
|
)
|
||||||
app.include_router(
|
app.include_router(
|
||||||
fastapi_users.get_users_router(),
|
fastapi_users.get_users_router(UserPublic, UserUpdate),
|
||||||
prefix="/users",
|
prefix="/users",
|
||||||
tags=["users"],
|
tags=["users"],
|
||||||
)
|
)
|
||||||
|
@ -12,9 +12,12 @@ class UserBase(BaseModel):
|
|||||||
class UserCreate(UserBase):
|
class UserCreate(UserBase):
|
||||||
password: str
|
password: str
|
||||||
|
|
||||||
# Properties to receive via API on update (optional, add later if needed)
|
# Properties to receive via API on update
|
||||||
# class UserUpdate(UserBase):
|
class UserUpdate(UserBase):
|
||||||
# password: Optional[str] = None
|
password: Optional[str] = None
|
||||||
|
is_active: Optional[bool] = None
|
||||||
|
is_superuser: Optional[bool] = None
|
||||||
|
is_verified: Optional[bool] = None
|
||||||
|
|
||||||
# Properties stored in DB
|
# Properties stored in DB
|
||||||
class UserInDBBase(UserBase):
|
class UserInDBBase(UserBase):
|
||||||
|
Loading…
Reference in New Issue
Block a user