This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import aiohttp
|
||||
from aiohttp.web import HTTPUnauthorized
|
||||
from strawberry.extensions import Extension
|
||||
|
||||
from orm.author import Author
|
||||
from services.db import local_session
|
||||
from settings import AUTH_URL
|
||||
|
||||
|
||||
@@ -61,3 +65,18 @@ async def check_auth(req) -> (bool, int | None):
|
||||
raise HTTPUnauthorized(message="Please, login first")
|
||||
|
||||
return False, None
|
||||
|
||||
|
||||
class LoginRequiredMiddleware(Extension):
|
||||
async def on_request_start(self):
|
||||
context = self.execution_context.context
|
||||
req = context.get("request")
|
||||
is_authenticated, user_id = await check_auth(req)
|
||||
if is_authenticated:
|
||||
with local_session() as session:
|
||||
author = session.query(Author).filter(Author.user == user_id).first()
|
||||
if author:
|
||||
context["author_id"] = author.id
|
||||
if user_id:
|
||||
context["user_id"] = user_id
|
||||
context["user_id"] = user_id or None
|
||||
|
Reference in New Issue
Block a user