ft_transcendence/accounts/views/login.py

23 lines
818 B
Python
Raw Normal View History

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-11-11 13:50:14 -05:00
from ..serializers.login import LoginSerializer
2023-11-11 13:50:14 -05:00
class LoginView(APIView):
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)
return Response('user connected', status=status.HTTP_200_OK)