Added DB connection and started creating api calls for the pages

This commit is contained in:
gulimabr
2025-11-30 15:17:23 -03:00
parent b5381ae376
commit bbbe65067b
20 changed files with 1403 additions and 152 deletions

View File

@@ -1,9 +1,11 @@
from fastapi import Depends, HTTPException, status, Request
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastapi.responses import RedirectResponse, JSONResponse
from sqlalchemy.ext.asyncio import AsyncSession
from src.models import TokenResponse, UserInfo
from src.service import AuthService
from src.service import AuthService, UserService
from src.config import get_settings
from src.database import get_db
# Initialize HTTPBearer security dependency
bearer_scheme = HTTPBearer()
@@ -35,13 +37,15 @@ class AuthController:
}
@staticmethod
def login(keycode: str, request: Request) -> RedirectResponse:
async def login(keycode: str, request: Request, db: AsyncSession) -> RedirectResponse:
"""
Authenticate user, set HTTP-only cookie, and redirect to frontend.
Authenticate user, provision in database if needed, set HTTP-only cookie,
and redirect to frontend.
Args:
keycode (str): The authorization code from Keycloak.
request (Request): The FastAPI request object.
db (AsyncSession): Database session for user provisioning.
Raises:
HTTPException: If the authentication fails.
@@ -50,7 +54,8 @@ class AuthController:
RedirectResponse: Redirects to frontend with cookie set.
"""
# Authenticate the user using the AuthService
access_token = AuthService.authenticate_user(keycode, request)
token_response = AuthService.authenticate_user(keycode, request)
access_token = token_response.get("access_token")
if not access_token:
raise HTTPException(
@@ -58,6 +63,10 @@ class AuthController:
detail="Authentication failed",
)
# Provision user in database (JIT provisioning)
# This creates the user if they don't exist
user_id, is_new_user = await UserService.provision_user_on_login(access_token, db)
# Create redirect response to frontend
response = RedirectResponse(
url=f"{settings.frontend_url}/dashboard",