auth.py 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. from django.conf import settings
  2. from django.utils.translation import ugettext as _
  3. from django.views.decorators.cache import never_cache
  4. from django.views.decorators.csrf import csrf_protect
  5. from django.views.decorators.debug import sensitive_post_parameters
  6. from rest_framework import status
  7. from rest_framework.decorators import api_view
  8. from rest_framework.response import Response
  9. from misago.users.decorators import (deny_authenticated, deny_guests,
  10. deny_banned_ips)
  11. from misago.users.forms.auth import AuthenticationForm
  12. @sensitive_post_parameters()
  13. @api_view(['POST'])
  14. @never_cache
  15. @deny_authenticated
  16. @csrf_protect
  17. @deny_banned_ips
  18. def authenticate(request):
  19. form = AuthenticationForm(request, data=request.data)
  20. if form.is_valid():
  21. return Response()
  22. else:
  23. error = form.errors.as_data()['__all__'][0]
  24. return Response({
  25. 'detail': error.messages[0],
  26. 'code': error.code
  27. }, status=status.HTTP_400_BAD_REQUEST)