# 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.generics import GenericAPIView from rest_framework.response import Response from django.views.decorators.csrf import csrf_exempt from django.contrib.auth.models import User from rest_framework.authtoken.models import Token from rest_framework.permissions import AllowAny, IsAuthenticated from rest_framework.authentication import TokenAuthentication from rest_framework_simplejwt.authentication import JWTAuthentication from rest_framework_simplejwt.tokens import RefreshToken from .serializers import * class UserRegistrationView(GenericAPIView): permission_classes = (AllowAny,) serializer_class = UserRegistrationSerializer def post(self, request, *args, **kwargs): serializer = UserRegistrationSerializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.save() token = RefreshToken.for_user(user) data = serializer.data data["tokens"] = {"refresh": str(token), "access": str(token.access_token)} return Response(data, status=status.HTTP_201_CREATED) # 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(GenericAPIView): permission_classes = (AllowAny,) serializer_class = UserLoginSerializer def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.validated_data serializer = CustomUserSerializer(user) token = RefreshToken.for_user(user) data = serializer.data data["tokens"] = {"refresh": str(token), "access": str(token.access_token)} return Response(data, status=status.HTTP_200_OK) # class UserLoginView(APIView): # authentication_classes = [JWTAuthentication] # permission_classes = [IsAuthenticated] # def get(self, request): # content = {'message': 'Hello, World!'} # return Response(content) # class UserLoginView(APIView): # authentication_classes = [JWTAuthentication] # permission_classes = [IsAuthenticated] # def post(self, request): # # Extract the token from the Authorization header # print(request) # auth_header = request.headers.get('Authorization') # if auth_header is None: # return Response({"error": # "Authorization header missing"}, # status=status.HTTP_401_UNAUTHORIZED) # try: # # The token is expected to be in the format "Bearer " # token_key = auth_header.split(' ')[1] # token = Token.objects.get(key=token_key) # user = token.user # return Response({"message": "Login successful", "user_id": user.id, "username": user.username}, status=status.HTTP_200_OK) # except (Token.DoesNotExist, IndexError): # return Response({"error": "Invalid token"}, status=status.HTTP_401_UNAUTHORIZED) class UserLogoutView(GenericAPIView): permission_classes = (IsAuthenticated,) def post(self, request, *args, **kwargs): try: refresh_token = request.data("refresh") token = RefreshToken(refresh_token) token.blacklist() return Response( status = status.HTTP_205_RESET_CONTENT) except Exception as e: return Response( status = status.HTTP_400_BAD_REQUEST) # def post(self, request): # logout(request) # return Response({"message": "Logout successful"}, status=status.HTTP_200_OK)