Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a section in docs about decorators #67

Merged
merged 4 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/67.doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a section about protecting views properly using the appropriate decorator for function based view and variable for class based views.
25 changes: 25 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,31 @@ Now in order to access protected api urls you must include the `Authorization: B
$ curl -H "Authorization: Bearer <your_token>" http://localhost:8000/protected-url/
```

In addition to adding the Authorization: Bearer in your requests, make sure that you have decorated your views properly. For this, import the `JSONWebTokenAuthentication` authentication class from `rest_framework_jwt.authentication`
fitodic marked this conversation as resolved.
Show resolved Hide resolved

```python
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
```
If you are using django's function based views, then you should decorate your views with the authentication_classes decorator containing JSONWebTokenAuthentication as follows:

```python
...
@authentication_classes([JSONWebTokenAuthentication])
def protectedView(request):
...
```

In case you are using class based views, you should override the `authentication_classes` attribute in the view like so:
```python
class ExampleView(APIView):
...
authentication_classes = [JSONWebTokenAuthentication]
...
```

This is necessary if the `JSONWebTokenAuthentication` class wasn't added to the `DEFAULT_AUTHENTICATION_CLASSES`, or if JWT authentication is needed *only* on certain views. If you have added it to `DEFAULT_AUTHENTICATION_CLASSES`, then the views will be authenticated using JWT authentication automatically.


## Refresh Token
If `JWT_ALLOW_REFRESH` is True, **non-expired** tokens can be "refreshed" to obtain a brand new token with renewed expiration time. Add a URL pattern like this:
```python
Expand Down