You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to know, how would go if you wanted to setup a route, which can be called when you are authenticated (using JWT for example), so you get the current user. But you can also allow unauthenticated User and get None instead of the current_user.
The problem is that current_user_from_token is called after the JWTCookieAuth checks for a JWT token. So if there's no token it raises a 401.
And if I set exclude_from_auth=True on my route, even if the user is authenticated I get current_user == None.
asyncdefcurrent_user_from_token(token: Token, connection: ASGIConnection[Any, Any, Any, Any]) ->User|None:
"""Lookup current user from local JWT token. Fetches the user information from the database Args: token (str): JWT Token Object connection (ASGIConnection[Any, Any, Any, Any]): ASGI connection. Returns: User: User record mapped to the JWT identifier """service=awaitanext(provide_users_service(alchemy.provide_session(connection.app.state, connection.scope)))
user=awaitservice.get_one_or_none(email=token.sub)
returnuserifuseranduser.is_activeelseNoneauth=JWTCookieAuth[User, ...](
retrieve_user_handler=current_user_from_token,
token_secret=settings.app.SECRET_KEY,
default_token_expiration=timedelta(days=7),
exclude=[
constants.OPENAPI_SCHEMA,
constants.HEALTH_ENDPOINT,
"/login",
"/signup",
"/oauth2",
],
)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to know, how would go if you wanted to setup a route, which can be called when you are authenticated (using JWT for example), so you get the current user. But you can also allow unauthenticated User and get None instead of the current_user.
The problem is that
current_user_from_token
is called after the JWTCookieAuth checks for a JWT token. So if there's no token it raises a 401.And if I set
exclude_from_auth=True
on my route, even if the user is authenticated I getcurrent_user == None
.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions