From e146e2d08deddf9b924c7657f04db28d71f34221 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sat, 28 Jun 2025 10:40:59 +0100 Subject: [PATCH] More detailed documentation for `current_user` --- docs/extensions.rst | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/extensions.rst b/docs/extensions.rst index 69904f0..dea61a3 100644 --- a/docs/extensions.rst +++ b/docs/extensions.rst @@ -420,13 +420,13 @@ be protected with the ``auth.optional`` decorator:: @app.route('/') @auth.optional async def index(request): - if g.current_user: + if request.g.current_user: return f'Hello, {request.g.current_user}!' else: return 'Hello, anonymous user!' -As shown in the example, a route can check ``g.current_user`` to determine if -the user is authenticated or not. +As shown in the example, a route can check ``request.g.current_user`` to +determine if the user is authenticated or not. Token Authentication ^^^^^^^^^^^^^^^^^^^^ @@ -446,7 +446,8 @@ or ``None`` if the token is invalid or expired:: return load_user_from_token(token) 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('/') @auth @@ -458,7 +459,7 @@ Optional authentication can also be used with tokens:: @app.route('/') @auth.optional async def index(request): - if g.current_user: + if request.g.current_user: return f'Hello, {request.g.current_user}!' else: return 'Hello, anonymous user!'