More detailed documentation for current_user

This commit is contained in:
Miguel Grinberg
2025-06-28 10:40:59 +01:00
parent dc61470fa9
commit e146e2d08d

View File

@@ -420,13 +420,13 @@ be protected with the ``auth.optional`` decorator::
@app.route('/') @app.route('/')
@auth.optional @auth.optional
async def index(request): async def index(request):
if g.current_user: if request.g.current_user:
return f'Hello, {request.g.current_user}!' return f'Hello, {request.g.current_user}!'
else: else:
return 'Hello, anonymous user!' return 'Hello, anonymous user!'
As shown in the example, a route can check ``g.current_user`` to determine if As shown in the example, a route can check ``request.g.current_user`` to
the user is authenticated or not. determine if the user is authenticated or not.
Token Authentication Token Authentication
^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
@@ -446,7 +446,8 @@ or ``None`` if the token is invalid or expired::
return load_user_from_token(token) return load_user_from_token(token)
As with Basic authentication, the ``auth`` instance is used as a decorator to As with Basic authentication, the ``auth`` instance is used as a decorator to
protect your routes:: protect your routes, and the authenticated user is accessible from the request
object as ``request.g.current_user``::
@app.route('/') @app.route('/')
@auth @auth
@@ -458,7 +459,7 @@ Optional authentication can also be used with tokens::
@app.route('/') @app.route('/')
@auth.optional @auth.optional
async def index(request): async def index(request):
if g.current_user: if request.g.current_user:
return f'Hello, {request.g.current_user}!' return f'Hello, {request.g.current_user}!'
else: else:
return 'Hello, anonymous user!' return 'Hello, anonymous user!'