This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
from functools import wraps
|
||||
import aiohttp
|
||||
from aiohttp.web import HTTPUnauthorized
|
||||
|
||||
from orm.author import Author
|
||||
from services.db import local_session
|
||||
from settings import AUTH_URL
|
||||
|
||||
|
||||
@@ -62,27 +58,6 @@ async def check_auth(req) -> (bool, int | None):
|
||||
except Exception as e:
|
||||
# Handling and logging exceptions during authentication check
|
||||
print(f"[services.auth] {e}")
|
||||
raise HTTPUnauthorized(message="Please, login first")
|
||||
|
||||
return False, None
|
||||
|
||||
|
||||
def login_required(f):
|
||||
@wraps(f)
|
||||
async def decorated_function(*args, **kwargs):
|
||||
info = args[1]
|
||||
context = info.context
|
||||
req = context.get("request")
|
||||
is_authenticated, user_id = await check_auth(req)
|
||||
if not is_authenticated:
|
||||
raise HTTPUnauthorized(text="Please, login first")
|
||||
else:
|
||||
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
|
||||
|
||||
return await f(*args, **kwargs)
|
||||
|
||||
return decorated_function
|
||||
|
Reference in New Issue
Block a user