# Path: server/core/serializers/user.py
# class LoggedUserSerializer(BaseUserSerializer):
#     pass
# 
# Path: server/authentication/serializers/signin.py
# class SigninSerializer(serializers.Serializer):
#     username = serializers.CharField(max_length=100)
#     password = serializers.CharField(max_length=50)
# 
#     def to_representation(self, instance):
#         return dict(instance, **{'email': instance['username']})
# 
#     def authenticate(self):
#         user = authenticate(**self.data)
#         if user is None:
#             return
#         login(self.context['request'], user)
#         assert user.is_authenticated()
#         return user


from django.shortcuts import render, redirect
from rest_framework import views, status
from rest_framework.response import Response
from server.core.serializers import LoggedUserSerializer
from ..serializers import SigninSerializer




class SigninView(views.APIView):
    def post(self, request):
        if request.user.is_authenticated():
            return redirect('index')

        serializer = SigninSerializer(data=request.data, context={'request': request})
        if not serializer.is_valid():
            return Response(status=400, data=serializer.error_messages)

        user = serializer.authenticate()

# Path: server/core/serializers/user.py
# class LoggedUserSerializer(BaseUserSerializer):
#     pass
# 
# Path: server/authentication/serializers/signin.py
# class SigninSerializer(serializers.Serializer):
#     username = serializers.CharField(max_length=100)
#     password = serializers.CharField(max_length=50)
# 
#     def to_representation(self, instance):
#         return dict(instance, **{'email': instance['username']})
# 
#     def authenticate(self):
#         user = authenticate(**self.data)
#         if user is None:
#             return
#         login(self.context['request'], user)
#         assert user.is_authenticated()
#         return user


# Path: server/authentication/views/signin.py
from django.shortcuts import render, redirect
from rest_framework import views, status
from rest_framework.response import Response
from server.core.serializers import LoggedUserSerializer
from ..serializers import SigninSerializer





class SigninView(views.APIView):
    def post(self, request):
        if request.user.is_authenticated():
            return redirect('index')

        serializer = SigninSerializer(data=request.data, context={'request': request})
        if not serializer.is_valid():
            return Response(status=400, data=serializer.error_messages)

        user = serializer.authenticate()

        if user: