0

I am trying to login to my custom LoginView using curl request but I a getting the following error:

{"detail":"Authentication credentials were not provided."}.

But the default login API(api-token-auth) is working fine.

LoginView:

class LoginView(views.APIView):
    def post(self, request, format=None):
        data = json.loads(request.body)

        email = data.get('email', None)
        password = data.get('password', None)

        account = authenticate(email=email, password=password)

        if account is not None:
            if account.is_active:
                login(request, account)
                token = get_jwt_token(account)
                return Response(token)
            else:
                return Response({
                    'status': 'Unauthorized',
                    'message': 'This account has been disabled.'
                }, status=status.HTTP_401_UNAUTHORIZED)
        else:
            return Response({
                'status': 'Unauthorized',
                'message': 'Username/password combination invalid.'
            }, status=status.HTTP_401_UNAUTHORIZED)
ilse2005
  • 11,189
  • 5
  • 51
  • 75
Puneet
  • 197
  • 1
  • 17
  • Is this your full view? I can't see this msg: `Authentication credentials were not provided.` – ilse2005 Mar 14 '16 at 15:45
  • Yes, this is my full view. This message is being returned by DRF authentication backend. It was permissions issue. I allowed anyone to login. Its working fine now. – Puneet Mar 14 '16 at 15:47
  • Add token in url like localhost:8000/api/users/ -H Authorization : Token – GrvTyagi Mar 14 '16 at 15:49
  • But token is returned after successful login and here I am trying to login. – Puneet Mar 14 '16 at 15:50
  • then just use @allowany decorator on class view, for deep follow http://www.django-rest-framework.org/api-guide/permissions/ – GrvTyagi Mar 14 '16 at 15:51
  • I couldn't find any such decorator. But yes landed on http://www.django-rest-framework.org/api-guide/permissions/#setting-the-permission-policy – Puneet Mar 14 '16 at 15:54
  • Are you sure, there's such decorator available as I couldn't find it and it gave error when I tried. – Puneet Mar 14 '16 at 16:02
  • 1
    You can find an example here http://stackoverflow.com/questions/16857450/how-to-register-users-in-django-rest-framework please read docs. carefully :) – GrvTyagi Mar 14 '16 at 16:04
  • you can also up-vote comments if you find some use-full information's :) ;) – GrvTyagi Mar 14 '16 at 16:17
  • Here, decorators can not be applied as these are class views not functional views. – Puneet Mar 14 '16 at 16:20

0 Answers0