This commit is contained in:
parent
99349dcad6
commit
f51d7539eb
|
@ -6,7 +6,7 @@ from resolvers import get_author_id
|
||||||
from settings import AUTH_URL
|
from settings import AUTH_URL
|
||||||
|
|
||||||
|
|
||||||
async def check_auth(req) -> (int | None, bool):
|
async def check_auth(req) -> (bool, int| None):
|
||||||
token = req.headers.get("Authorization")
|
token = req.headers.get("Authorization")
|
||||||
if token:
|
if token:
|
||||||
# Logging the authentication token
|
# Logging the authentication token
|
||||||
|
@ -44,9 +44,7 @@ async def check_auth(req) -> (int | None, bool):
|
||||||
if user_id:
|
if user_id:
|
||||||
# Logging the retrieved user ID
|
# Logging the retrieved user ID
|
||||||
print(f"User ID retrieved: {user_id}")
|
print(f"User ID retrieved: {user_id}")
|
||||||
# Fetching author information
|
return True, user_id
|
||||||
author = get_author_id(None, None, user_id)
|
|
||||||
return author.id, True
|
|
||||||
else:
|
else:
|
||||||
# Logging when no user ID is found in the response
|
# Logging when no user ID is found in the response
|
||||||
print("No user ID found in the response")
|
print("No user ID found in the response")
|
||||||
|
@ -69,13 +67,13 @@ def login_required(f):
|
||||||
print(context)
|
print(context)
|
||||||
req = context.get("request")
|
req = context.get("request")
|
||||||
# Performing authentication check
|
# Performing authentication check
|
||||||
is_authenticated, author_id = await check_auth(req)
|
is_authenticated, user_id = await check_auth(req)
|
||||||
if not is_authenticated:
|
if not is_authenticated:
|
||||||
# Raising an exception if the user is not authenticated
|
# Raising an exception if the user is not authenticated
|
||||||
raise HTTPUnauthorized(text="Please, login first")
|
raise HTTPUnauthorized(text="Please, login first")
|
||||||
else:
|
else:
|
||||||
# Adding user_id to the context
|
# Adding user_id to the context
|
||||||
context["author_id"] = author_id
|
context["user_id"] = user_id
|
||||||
|
|
||||||
# If the user is authenticated, execute the resolver
|
# If the user is authenticated, execute the resolver
|
||||||
return await f(*args, **kwargs)
|
return await f(*args, **kwargs)
|
||||||
|
@ -88,13 +86,13 @@ def auth_request(f):
|
||||||
async def decorated_function(*args, **kwargs):
|
async def decorated_function(*args, **kwargs):
|
||||||
req = args[0]
|
req = args[0]
|
||||||
# Performing authentication check
|
# Performing authentication check
|
||||||
is_authenticated, author_id = await check_auth(req)
|
is_authenticated, user_id = await check_auth(req)
|
||||||
if not is_authenticated:
|
if not is_authenticated:
|
||||||
# Raising HTTPUnauthorized exception if the user is not authenticated
|
# Raising HTTPUnauthorized exception if the user is not authenticated
|
||||||
raise HTTPUnauthorized(text="Please, login first")
|
raise HTTPUnauthorized(text="Please, login first")
|
||||||
else:
|
else:
|
||||||
# Modifying the req with the author_id
|
# Modifying the req with the author_id
|
||||||
req["author_id"] = author_id
|
req["user_id"] = user_id
|
||||||
return await f(*args, **kwargs)
|
return await f(*args, **kwargs)
|
||||||
|
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
|
Loading…
Reference in New Issue
Block a user