email and pin is not being stored in session

I’m trying to change the user pin after user forgotten his pin but after verifcation of reset code when use wants to enter new pin and confirm new the the data before set new pin like email and reset code is not being saved in session here is the code of verifyPinCode and SetNewPinCode and another thing is when I only want to add 2 fields in postman payload while testing apis new_pin and confirm new pin.

verifyPinCodeView

yourclass VerifyResetCodeView(APIView):
    permission_classes = [permissions.AllowAny]

    def post(self, request):
        serializer = VerifyResetCodeSerializer(data=request.data)
        if serializer.is_valid():
            reset_code = serializer.validated_data.get('reset_code')

            try:
                pin_reset = PinResetCode.objects.get(reset_code=reset_code)
            except PinResetCode.DoesNotExist:
                return Response({'error': 'Invalid reset code.'}, status=status.HTTP_400_BAD_REQUEST)
            
            if pin_reset.is_expired():
                return Response({'error': 'Reset code expired.'}, status=status.HTTP_400_BAD_REQUEST)

            # Store reset code and email in the session
            request.session['reset_code'] = reset_code
            request.session['email'] = pin_reset.email
            request.session.save()  # Explicitly save the session

            # Debugging statements
            print(f"Storing - Reset Code: {reset_code}, Email: {pin_reset.email}")
            print(f"Stored Session Data - Reset Code: {request.session.get('reset_code')}, Email: {request.session.get('email')}")

            return Response({'message': 'Reset code is Verified. You can now set your new PIN.'}, status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

SetNewPin code view

class SetNewPinView(APIView):
    permission_classes = [permissions.AllowAny]

    def post(self, request):
        serializer = SetNewPinSerializer(data=request.data)
        if serializer.is_valid():
            new_pin = serializer.validated_data.get('new_pin')
            confirm_new_pin = serializer.validated_data.get('confirm_new_pin')

            # Retrieve reset code and email from session
            reset_code = request.session.get('reset_code')
            email = request.session.get('email')

            # Debugging information
            print(f"Session data - Reset Code: {reset_code}, Email: {email}")

            # Ensure reset code and email are present
            if not reset_code or not email:
                return Response({'error': 'Reset code and email not found in session.'}, status=status.HTTP_400_BAD_REQUEST)
            
            # Check if new PINs match
            if new_pin != confirm_new_pin:
                return Response({'error': 'New PIN and confirm new PIN do not match.'}, status=status.HTTP_400_BAD_REQUEST)
            
            try:
                pin_reset = PinResetCode.objects.get(email=email, reset_code=reset_code)
            except PinResetCode.DoesNotExist:
                return Response({'error': 'Invalid reset code.'}, status=status.HTTP_400_BAD_REQUEST)
            
            if pin_reset.is_expired():
                return Response({'error': 'Reset code expired.'}, status=status.HTTP_400_BAD_REQUEST)
            
            user = get_object_or_404(Employee, email=email)
            user.set_password(new_pin)
            user.save()

            pin_reset.delete()

            # Clear session data
            request.session.pop('reset_code', None)
            request.session.pop('email', None)

            return Response({'message': 'PIN reset successful.'}, status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Response that I’m getting from postman while testing is this:

{
    "error": "Reset code and email not found in session."
}

I tried everything like added debugging steps everything. How to fix this?

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật