1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] email and pin is not being stored in session

Discussão em 'Python' iniciado por Stack, Setembro 14, 2024.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página