working auth + users systems

This commit is contained in:
c-d-p
2025-04-16 21:32:57 +02:00
parent 516adc606d
commit 18ddb2f332
56 changed files with 943 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,10 @@
# core/celery_app.py
from celery import Celery
from core.config import settings
celery = Celery(
"maia",
broker=f"redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}/0",
backend=f"redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}/1",
include=["modules.auth.tasks"], # List all task modules here
)

21
backend/core/config.py Normal file
View File

@@ -0,0 +1,21 @@
# core/config.py
from pydantic_settings import BaseSettings
from os import getenv
from dotenv import load_dotenv
load_dotenv() # Load .env file
class Settings(BaseSettings):
DB_URL: str = "postgresql://maia:maia@localhost:5432/maia"
REDIS_HOST: str = "localhost"
REDIS_PORT: int = 6379
JWT_ALGORITHM: str = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
REFRESH_TOKEN_EXPIRE_DAYS: int = 7
PEPPER: str = getenv("PEPPER", "")
JWT_SECRET_KEY: str = getenv("JWT_SECRET_KEY", "")
settings = Settings()

36
backend/core/database.py Normal file
View File

@@ -0,0 +1,36 @@
# core/database.py
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session, declarative_base
from typing import Generator
from core.config import settings
Base = declarative_base() # Used for models
_engine = None
_SessionLocal = None
def get_engine():
global _engine
if _engine is None:
if not settings.DB_URL:
raise ValueError("DB_URL is not set in Settings.")
print(f"Connecting to database at {settings.DB_URL}")
_engine = create_engine(settings.DB_URL)
Base.metadata.create_all(_engine) # Create tables here
return _engine
def get_sessionmaker():
global _SessionLocal
if _SessionLocal is None:
engine = get_engine()
_SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
return _SessionLocal
def get_db() -> Generator[Session, None, None]:
SessionLocal = get_sessionmaker()
db = SessionLocal()
try:
yield db
finally:
db.close()

View File

@@ -0,0 +1,27 @@
from fastapi import HTTPException
from starlette.status import (
HTTP_400_BAD_REQUEST,
HTTP_401_UNAUTHORIZED,
HTTP_403_FORBIDDEN,
HTTP_404_NOT_FOUND,
HTTP_500_INTERNAL_SERVER_ERROR,
HTTP_409_CONFLICT,
)
def bad_request_exception(detail: str = "Bad Request"):
return HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=detail)
def unauthorized_exception(detail: str = "Unauthorized"):
return HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail=detail)
def forbidden_exception(detail: str = "Forbidden"):
return HTTPException(status_code=HTTP_403_FORBIDDEN, detail=detail)
def not_found_exception(detail: str = "Not Found"):
return HTTPException(status_code=HTTP_404_NOT_FOUND, detail=detail)
def internal_server_error_exception(detail: str = "Internal Server Error"):
return HTTPException(status_code=HTTP_500_INTERNAL_SERVER_ERROR, detail=detail)
def conflict_exception(detail: str = "Conflict"):
return HTTPException(status_code=HTTP_409_CONFLICT, detail=detail)