- Author:
- techiediaries
- Posted:
- August 7, 2017
- Language:
- Python
- Version:
- 1.7
- Score:
- 1 (after 1 ratings)
This is an example of Django auth with JWT tokens, you can find how to add jwt auth to Django Rest Framework in this tutorial
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | from rest_framework import status
from rest_framework.generics import CreateAPIView
from rest_framework.response import Response
from rest_framework_jwt.settings import api_settings
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
class CreateUserView(CreateAPIView):
model = User.objects.all()
permission_classes = [
permissions.AllowAny # Or anon users can't register
]
serializer_class = UserSerializer
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
user = self.model.get(username=serializer.data['username'])
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
return Response(
{
'confirmation_url': reverse(
'activate-user', args=[token], request=request
)
},
status=status.HTTP_201_CREATED, headers=headers
)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.