ft_transcendence/accounts/views/login.py

24 lines
828 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):
data = request.POST
serializer = LoginSerializer(data=data)
if 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)