# main/views.py from django.contrib.auth import authenticate, login, logout from rest_framework import status from rest_framework.views import APIView from rest_framework.response import Response class UserRegistrationView(APIView): def post(self, request): serializer = UserRegistrationSerializer(data=request.data) if serializer.is_valid(): user = serializer.save() return Response({"id": user.id, "username": user.username}, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) class UserLoginView(APIView): def post(self, request): username = request.data.get('username') password = request.data.get('password') user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return Response({"message": "Login successful"}, status=status.HTTP_200_OK) return Response({error: "Invalid credentials"}, status=status.HTTP_401_UNAUTHORIZED) class UserLogoutView(APIView): def post(self, request): logout(request) return Response({"message": "Logout successful"}, status=status.HTTP_200_OK)