Skip to content

Commit 8b18943

Browse files
authored
Merge pull request #47 from carlegbert/jwtopt_docs
Documentation for jwt_optional decorator
2 parents 938d60d + cf4462d commit 8b18943

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

docs/basic_usage.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,27 @@ NOTE: Remember to change the secret key of your application, and insure that no
4040
one is able to view it. The json web tokens are signed with the secret key, so
4141
if someone gets that, they can create arbitrary tokens, and in essence log in
4242
as any user.
43+
44+
Partially protecting routes
45+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
46+
47+
There may be cases where you want to use one endpoint for both protected
48+
and unprotected data. In these situations, you can use the **jwt_optional**
49+
decorator. This will allow the view to be called whether or not a token
50+
is sent in the request, although if the token is expired or badly constructed,
51+
or if the header is improperly formatted or otherwise incorrect, an error
52+
will be returned.
53+
54+
.. code-block:: python
55+
56+
@app.route('/partially-protected', methods=['GET'])
57+
@jwt_optional
58+
def partially_protected():
59+
# If no JWT is sent in the request headers, get_jwt_identity()
60+
# will return None
61+
current_user = get_jwt_identity()
62+
if current_user:
63+
return jsonify({'hello_from': current_user}), 200
64+
65+
return jsonify({'hello_from': 'an anonymous user'}), 200
66+

0 commit comments

Comments
 (0)