2023-11-11 13:50:14 -05:00
|
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework import permissions, status
|
|
|
|
from django.http import HttpRequest
|
|
|
|
from django.contrib.auth import login
|
|
|
|
from rest_framework.authentication import SessionAuthentication
|
2023-10-25 09:35:24 -04:00
|
|
|
|
2023-11-11 13:50:14 -05:00
|
|
|
from ..serializers.login import LoginSerializer
|
2023-10-25 09:35:24 -04:00
|
|
|
|
2023-11-11 13:50:14 -05:00
|
|
|
class LoginView(APIView):
|
2023-10-31 16:14:24 -04:00
|
|
|
|
2023-11-11 13:50:14 -05:00
|
|
|
permission_classes = (permissions.AllowAny,)
|
|
|
|
authentication_classes = (SessionAuthentication,)
|
|
|
|
|
|
|
|
def post(self, request: HttpRequest):
|
2023-11-13 07:22:10 -05:00
|
|
|
data = request.data
|
2023-11-11 13:50:14 -05:00
|
|
|
serializer = LoginSerializer(data=data)
|
2023-11-13 07:22:10 -05:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
user = serializer.get_user(data)
|
|
|
|
if user is None:
|
|
|
|
return Response({'user': ['Username or password wrong.']}, status.HTTP_200_OK)
|
|
|
|
login(request, user)
|
2023-12-06 09:19:41 -05:00
|
|
|
return Response({'id': user.pk}, status=status.HTTP_200_OK)
|