views.py 937 B

12345678910111213141516171819202122232425
  1. from .serializers import MyTokenObtainPairSerializer
  2. from django.http import HttpResponse, JsonResponse
  3. from django.views.decorators.csrf import csrf_exempt
  4. from rest_framework.decorators import api_view, permission_classes,authentication_classes
  5. from rest_framework_simplejwt.views import TokenObtainPairView
  6. from .forms import RegisterForm
  7. from .models import CustomUser
  8. class MyTokenObtainPairView(TokenObtainPairView):
  9. serializer_class = MyTokenObtainPairSerializer
  10. @csrf_exempt
  11. def register(request):
  12. if request.method == 'POST':
  13. form = RegisterForm(request.POST)
  14. if form.is_valid():
  15. newUser = form.save()
  16. return JsonResponse({"data":{"_id":newUser._id}}, safe=False)
  17. else:
  18. return JsonResponse({"errors":[{"message":"Невірні дані"}]}, safe=False)
  19. return JsonResponse({"errors":[{"message":"Invalid method, method must be POST"}]}, safe=False)