import os import uuid from fastapi import Request from sqladmin.authentication import AuthenticationBackend as AuthBackendAdmin from fastapi_users import FastAPIUsers from fastapi_users.authentication import CookieTransport, AuthenticationBackend from fastapi_users.authentication import JWTStrategy from fastapi_users.password import PasswordHelper from project.users.manager import get_user_manager from project.users.models import User from sqlalchemy import select cookie_transport = CookieTransport( cookie_name="real-estate", cookie_max_age=3600, cookie_path="/", cookie_domain=None, cookie_secure=False, cookie_httponly=False, cookie_samesite="lax", ) def get_jwt_strategy() -> JWTStrategy: return JWTStrategy(secret=os.getenv('SECRET'), lifetime_seconds=3600) auth_backend = AuthenticationBackend( name='jwt', transport=cookie_transport, get_strategy=get_jwt_strategy ) fastapi_users = FastAPIUsers[User, int]( get_user_manager, [auth_backend] ) class AdminAuth(AuthBackendAdmin): async def login(self, request: Request) -> bool: form = await request.form() username, password = form["username"], form["password"] if username == 'hectool24' and password == 'hectoolshopify2024@': request.session.update({"session": str(uuid.uuid4())}) return True return False async def logout(self, request: Request) -> bool: request.session.clear() return True async def authenticate(self, request: Request) -> bool: token = request.session.get("session") if not token: return False return True authentication_backend_admin = AdminAuth(secret_key=os.getenv('SECRET'))