1
votes

I have a project with AngularJS, JWT and django as back-end framework. JWT user authentication works OK.

At the same time I'm trying

  • Send $http.get('/myview/') from my angular service
  • To myview in django which is decorated with login_required.

After research I understand that it shouldn't work because request.session is not set.

So the question is how can I allow to obtain requests in this view only from authenticated users?

I have idea to create custom decorator in django that will check the token that will be sent within my get/post request from angular service but I have doubts that it will work.

1

1 Answers

3
votes

I've found the solution. I didn't think that rest framework permission classes can also work as decorator for a view.

from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated

@api_view(['GET'])
@permission_classes((IsAuthenticated, ))
def example_view(request, format=None):

http://www.django-rest-framework.org/api-guide/permissions/