fix getSession, fix getAuthor

This commit is contained in:
tonyrewin 2022-11-24 18:19:58 +03:00
parent 0ad10cffec
commit b2b8cf747f
4 changed files with 12 additions and 22 deletions

View File

@ -68,7 +68,7 @@ def login_required(func):
def permission_required(resource, operation, func): def permission_required(resource, operation, func):
@wraps(func) @wraps(func)
async def wrap(parent, info: GraphQLResolveInfo, *args, **kwargs): async def wrap(parent, info: GraphQLResolveInfo, *args, **kwargs):
# print('[auth.authenticate] login required for %r with info %r' % (func, info)) # debug only print('[auth.authenticate] permission_required for %r with info %r' % (func, info)) # debug only
auth: AuthCredentials = info.context["request"].auth auth: AuthCredentials = info.context["request"].auth
if not auth.logged_in: if not auth.logged_in:
return {"error": auth.error_message or "Please login"} return {"error": auth.error_message or "Please login"}

View File

@ -21,32 +21,23 @@ from resolvers.zine.profile import user_subscriptions
from settings import SESSION_TOKEN_HEADER from settings import SESSION_TOKEN_HEADER
@mutation.field("refreshSession") @mutation.field("getSession")
@login_required @login_required
async def get_current_user(_, info): async def get_current_user(_, info):
user = info.context["request"].user user = info.context["request"].user
# print(info.context["request"].headers) token = info.context["request"].headers.get("Authorization")
old_token = info.context["request"].headers.get("Authorization") if user and token:
user.lastSeen = datetime.now(tz=timezone.utc) user.lastSeen = datetime.now(tz=timezone.utc)
with local_session() as session: with local_session() as session:
session.add(user) session.add(user)
session.commit() session.commit()
token = await TokenStorage.create_session(user)
print("[resolvers.auth] new session token created")
if old_token:
payload = await TokenStorage.get(str(user.id) + '-' + str(old_token))
if payload:
print("[resolvers.auth] got session from old token: %r" % payload)
return { return {
"token": token, "token": token,
"user": user, "user": user,
"news": await user_subscriptions(user.slug), "news": await user_subscriptions(user.slug),
} }
return { else:
"token": token, raise OperationNotAllowed("No session token present in request, try to login")
"user": user,
"news": await user_subscriptions(user.slug),
}
@mutation.field("confirmEmail") @mutation.field("confirmEmail")

View File

@ -185,8 +185,7 @@ async def get_authors_all(_, _info):
async def get_author(_, _info, slug): async def get_author(_, _info, slug):
with local_session() as session: with local_session() as session:
author = session.query(User).join(ShoutAuthor).where(User.slug == slug).first() author = session.query(User).join(ShoutAuthor).where(User.slug == slug).first()
for author in author: author.stat = await get_author_stat(author.slug)
author.stat = await get_author_stat(author.slug)
return author return author

View File

@ -159,7 +159,7 @@ type Mutation {
markAsRead(chatId: String!, ids: [Int]!): Result! markAsRead(chatId: String!, ids: [Int]!): Result!
# auth # auth
refreshSession: AuthResult! getSession: AuthResult!
registerUser(email: String!, password: String, name: String): AuthResult! registerUser(email: String!, password: String, name: String): AuthResult!
sendLink(email: String!, lang: String): Result! sendLink(email: String!, lang: String): Result!
confirmEmail(token: String!): AuthResult! confirmEmail(token: String!): AuthResult!