2

So I followed the instructions from the answers on this thread: @csrf_exempt does not work on generic view based class

However, when I send POST requests via Postman, it still keeps throwing 403 error and I wonder what I am missing here. I tried the last answer(using braces.views and CsrfExemptMixin) as well and it still wouldn't work. Below is my code so far

import json
import jwt

from psr.settings                   import SECRET_KEY
from django.http                    import HttpResponse, JsonResponse
from django.contrib.auth.forms      import AuthenticationForm
from django.contrib                 import messages
from django.contrib.auth            import login, logout, authenticate
from django.contrib.auth            import views as auth_views
from django.utils.decorators        import method_decorator
from django.views.decorators.csrf   import csrf_exempt
from braces.views                   import CsrfExemptMixin

from .models import User



class LoginView(auth_views.LoginView):

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return super(LoginView, self).dispatch(request, *args, **kwargs)


    def post(self, request):
        form = AuthenticationForm(data = request.POST)
        if form.is_valid():
            user = authenticate(email=request.POST['email'], password=request.POST['password'])
            if user is not None:
                messages.add_message(request, messages.SUCCESS, "Welcome back, {}".format(user))

                login(request, user)

                token = jwt.encode({'id': user.id}, SECRET_KEY, algorithm='HS256').decode('utf-8')
                
                return JsonResponse({'token': token}, status=200) 

Am I missing something here?

Thanks a lot in advance!

Nick
  • 133
  • 6

0 Answers0