The resource /api-auth/login/ intended only for authentication in the browseble api. To use session authentication, you must first create a session. You must have a login resource that accepts user credentials and authenticates the user using the Django authentication system. When this resource is requested, the client will be the header of the cookie. Amd cookie should be used in subsequent requests.
curl -v -X POST https://example.com/api/user/login/ -d 'username=user&password=pass' ... > Set-Cookie: csrftoken=TqIuhp8oEP9VY32tUDcfQyUwn3cqpYCa; expires=Fri, 15-May-2015 12:48:57 GMT; Max-Age=31449600; Path=/ > Set-Cookie: sessionid=4yb4s456lbvd974oijbdha7k3l6g52q3; expires=Fri, 30-May-2014 12:48:57 GMT; Max-Age=1209600; Path=/
DRF also supports basic authentication. You can use it to authenticate the user and create a session. Here is an example:
from django.contrib.auth import login from rest_framework.authentication import BasicAuthentication, SessionAuthentication from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView class MyBasicAuthentication(BasicAuthentication): def authenticate(self, request): user, _ = super(MyBasicAuthentication, self).authenticate(request) login(request, user) return user, _ class ExampleView(APIView): authentication_classes = (SessionAuthentication, MyBasicAuthentication) permission_classes = (IsAuthenticated,) def get(self, request, format=None): content = { 'user': unicode(request.user), 'auth': unicode(request.auth),
Yattoff
source share