Spaces:
Running
Running
Sync DemoService df6fb3fa2031ffbdfa92f139dd710df5c19f2163
Browse files
api/auth-service/app/api/v1/endpoints/auth.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
from sqlalchemy import text
|
| 3 |
from sqlalchemy.exc import SQLAlchemyError
|
| 4 |
from sqlalchemy.orm import Session
|
| 5 |
-
import hmac
|
| 6 |
-
from fastapi import HTTPException
|
| 7 |
from jose import JWTError
|
| 8 |
from app.core.config import settings
|
| 9 |
from app.core.security import create_access_token, create_refresh_token, decode_token, decode_refresh_token, password_stamp
|
|
@@ -39,6 +40,23 @@ from app.services.auth import (
|
|
| 39 |
)
|
| 40 |
|
| 41 |
router = APIRouter(prefix="/auth", tags=["auth"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
def _ensure_profile_image_column(db: Session) -> None:
|
|
@@ -120,8 +138,18 @@ def email_verification_confirm_endpoint(
|
|
| 120 |
|
| 121 |
@router.post("/login", response_model=Token)
|
| 122 |
def login_endpoint(data: LoginRequest, db: Session = Depends(get_db)):
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
@router.post("/google", response_model=Token)
|
|
|
|
| 1 |
+
import hmac
|
| 2 |
+
import logging
|
| 3 |
+
|
| 4 |
+
from fastapi import APIRouter, Depends, HTTPException, status
|
| 5 |
from sqlalchemy import text
|
| 6 |
from sqlalchemy.exc import SQLAlchemyError
|
| 7 |
from sqlalchemy.orm import Session
|
|
|
|
|
|
|
| 8 |
from jose import JWTError
|
| 9 |
from app.core.config import settings
|
| 10 |
from app.core.security import create_access_token, create_refresh_token, decode_token, decode_refresh_token, password_stamp
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
router = APIRouter(prefix="/auth", tags=["auth"])
|
| 43 |
+
logger = logging.getLogger("uvicorn.error")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@router.get("/health/db", include_in_schema=False)
|
| 47 |
+
def auth_database_health(db: Session = Depends(get_db)):
|
| 48 |
+
"""Expose a sanitized database readiness check for deployment diagnosis."""
|
| 49 |
+
try:
|
| 50 |
+
db.execute(text("SELECT 1"))
|
| 51 |
+
return {"status": "ok", "database": "ready"}
|
| 52 |
+
except SQLAlchemyError as exc:
|
| 53 |
+
db.rollback()
|
| 54 |
+
root_error = getattr(exc, "orig", exc)
|
| 55 |
+
logger.error("Auth database readiness failed (%s)", type(root_error).__name__)
|
| 56 |
+
raise HTTPException(
|
| 57 |
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
| 58 |
+
detail={"code": "auth_database_unavailable"},
|
| 59 |
+
) from exc
|
| 60 |
|
| 61 |
|
| 62 |
def _ensure_profile_image_column(db: Session) -> None:
|
|
|
|
| 138 |
|
| 139 |
@router.post("/login", response_model=Token)
|
| 140 |
def login_endpoint(data: LoginRequest, db: Session = Depends(get_db)):
|
| 141 |
+
try:
|
| 142 |
+
access_token = login(db, data.email, data.password)
|
| 143 |
+
user = get_user_by_id(db, int(decode_token(access_token)["sub"]))
|
| 144 |
+
return session_tokens(user)
|
| 145 |
+
except SQLAlchemyError as exc:
|
| 146 |
+
db.rollback()
|
| 147 |
+
root_error = getattr(exc, "orig", exc)
|
| 148 |
+
logger.error("Login database lookup failed (%s)", type(root_error).__name__)
|
| 149 |
+
raise HTTPException(
|
| 150 |
+
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
| 151 |
+
detail={"code": "auth_database_unavailable"},
|
| 152 |
+
) from exc
|
| 153 |
|
| 154 |
|
| 155 |
@router.post("/google", response_model=Token)
|
api/auth-service/app/db/session.py
CHANGED
|
@@ -3,5 +3,14 @@ from sqlalchemy.orm import sessionmaker
|
|
| 3 |
|
| 4 |
from app.core.config import settings
|
| 5 |
|
| 6 |
-
engine = create_engine(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)
|
|
|
|
| 3 |
|
| 4 |
from app.core.config import settings
|
| 5 |
|
| 6 |
+
engine = create_engine(
|
| 7 |
+
settings.database_url,
|
| 8 |
+
pool_pre_ping=True,
|
| 9 |
+
pool_recycle=300,
|
| 10 |
+
pool_timeout=10,
|
| 11 |
+
connect_args={
|
| 12 |
+
"connect_timeout": 10,
|
| 13 |
+
"options": "-c statement_timeout=15000 -c lock_timeout=5000",
|
| 14 |
+
},
|
| 15 |
+
)
|
| 16 |
SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)
|