Is it possible to allow multiple attempts on AWS Cognito Passwordless?

I am working with Cognito Passwordless in AWS for my Ruby on Rails API project. I have created the 3 needed lambdas(DefineAuthChallenge, CreateAuthChallenge and VerifyAuthChallengeResponse) and it works fine but I want to allow the user to have multiple attempts to enter the correct verification code needed to login.

My problem is that if the user fails the first attempt in the second one, after entering the correct verification code, I always get the same message from Cognito(“Invalid session for the user”).

Digging a little bit more in the problem and checking the logs I realized that the lambda VerifyAuthChallengeResponse is only executed the first time and in the DefineAuthChallenge the second attempt is not beeing registered.

Any help?

My lambdas are something like this.

DefineAuthChallenge

def lambda_handler(event:, context:)
  puts "Event: #{event.inspect}"
  if event['triggerSource'] == 'DefineAuthChallenge_Authentication'
    session = event.dig('request', 'session') || []
    puts "Session: #{session.inspect}"

    if session.empty?
      # New request to log in
      puts "New request to log in"
      event['response'] = {
        'challengeName' => 'CUSTOM_CHALLENGE',
        'issueTokens' => false,
        'failAuthentication' => false
      }
    elsif session.length >= 2 && !session.last['challengeResult']
      # User has incorrectly entered OTP 2 times
      puts "User has incorrectly entered OTP 2 times"
      event['response'] = {
        'issueTokens' => false,
        'failAuthentication' => true
      }
    elsif session.any? { |attempt| attempt['challengeName'] == 'CUSTOM_CHALLENGE' && attempt['challengeResult'] }
      # User correctly entered OTP
      puts "User correctly entered OTP"
      event['response'] = {
        'issueTokens' => true,
        'failAuthentication' => false
      }
    else
      # User incorrectly entered OTP - resend code
      puts "User incorrectly entered OTP - resend code"
      event['response'] = {
        'challengeName' => 'CUSTOM_CHALLENGE',
        'issueTokens' => false,
        'failAuthentication' => false
      }
    end
  end

  puts "Response: #{event['response'].inspect}"
  event
end

CreateAuthChallenge

require 'aws-sdk-ses'

def lambda_handler(event:, context:)
  puts "Event: #{event.inspect}"

  if event['triggerSource'] == 'CreateAuthChallenge_Authentication'
    code = generate_code
    send_verification_code(event['request']['userAttributes']['email'], code)

    event['response'] = {
      'publicChallengeParameters' => { 'email' => event['request']['userAttributes']['email'] },
      'privateChallengeParameters' => { 'secretLoginCode' => code },
      'challengeMetadata' => 'CODE_DELIVERY'
    }
  end

  puts "Response: #{event['response'].inspect}"
  event
end

def generate_code
  rand(100000..999999).to_s
end

def send_verification_code(email, code)
  ses = Aws::SES::Client.new(region: '*******')
  
  user_status_message = "Welcome!"
  
  ses.send_email({
    destination: { to_addresses: [email] },
    message: {
      body: {
        text: { 
          charset: 'UTF-8', 
          data: "#{user_status_message}nYour verification code is #{code}" 
        }
      },
      subject: { 
        charset: 'UTF-8', 
        data: 'Your verification code' 
      }
    },
    source: '***@***.***'
  })
end

VerifyAuthChallengeResponse

def lambda_handler(event:, context:)
  puts "Event: #{event.inspect}"
  
  if event['triggerSource'] == 'VerifyAuthChallengeResponse_Authentication'
    expected_answer = event['request']['privateChallengeParameters']['secretLoginCode']
    provided_answer = event['request']['challengeAnswer']
    
    puts "Expected Answer: #{expected_answer}"
    puts "Provided Answer: #{provided_answer}"
    
    if provided_answer == expected_answer
      event['response']['answerCorrect'] = true
      puts "Answer is correct"
    else
      event['response']['answerCorrect'] = false
      puts "Answer is incorrect"
    end
  end
  
  puts "Response: #{event['response'].inspect}"
  event
end

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