-2

I want to create global authentication with middleware django restframework which check every request user authenticate or not.

I don't want to add code on every view class like this

@permission_classes([IsAuthenticated]) class UserProfile(APIView):

Anil Yadav
  • 241
  • 4
  • 15
  • what did you try? if you haven't tried anything then you can start by following this SO question [How to set up custom middleware in Django](https://stackoverflow.com/questions/18322262/how-to-set-up-custom-middleware-in-django) – Nalin Dobhal Nov 05 '19 at 08:59
  • 2
    Can you please explain *what* you exactly aim to do, and what you tried and is not working? – Willem Van Onsem Nov 05 '19 at 09:09
  • I want to implement authentication with middleware not add code every view class @permission_classes([IsAuthenticated]) class UserProfile(APIView):pass I want to create global authentication with middleware – Anil Yadav Nov 05 '19 at 09:17
  • 3
    [duplicate](https://stackoverflow.com/q/48407790/1324033) – Sayse Nov 05 '19 at 09:22

1 Answers1

1

You just need to set the permissions policy for DRF in your settings.py file. Here the Docs

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ]
}
Jagjeet Singh
  • 547
  • 5
  • 11
  • 1
    Then you create a middleware for you django app so all routes can required authentication – Linh Nguyen Nov 05 '19 at 09:52
  • 1
    @AnilYadav No, You don't need to write in your every view class or function. when you set new permission classes through class attribute or decorators you're telling the view to ignore the default list set over the settings.py file. – Jagjeet Singh Nov 05 '19 at 10:01
  • Thanks it working and which url not need authenticate we add @permission_classes([AllowAny]) – Anil Yadav Nov 05 '19 at 10:08