【问题标题】:rails devise recaptcha prepend_before_action :check_captcha not working?rails devise recaptcha prepend_before_action :check_captcha 不工作?
【发布时间】:2021-10-11 22:36:59
【问题描述】:

发生的情况是即使验证码验证失败,用户也可以登录这是我关于用户的路线:

      devise_for :users, controllers: {
        sessions: 'users/sessions',
        registrations: 'users/registrations',
        passwords: 'users/passwords'
      }
    resources :users

这是我的相关 app/controllers/users/sessions_controller.rb 代码:


    prepend_before_action :check_captcha, only: [:create]
    
        def check_captcha
          return if !verify_recaptcha # verify_recaptcha(action: 'login') for v3
    
          self.resource = resource_class.new sign_in_params
    
          respond_with_navigational(resource) do
            flash.discard(:recaptcha_error) # We need to discard flash to avoid showing it on the next page reload
            render :new
          end
        end

我的 config/rechaptcha.rb 包含环境检查并根据环境传递不同的密钥:


    Recaptcha.configure do |config|
      if Rails.env.development?
          config.site_key  = '6L..Bg'
          config.secret_key = '6L..WG'
      elsif Rails.env.production?
          config.site_key  = '6L...b'
          config.secret_key = '6L.gy'
      end
    end

【问题讨论】:

    标签: ruby-on-rails devise recaptcha


    【解决方案1】:

    如果验证码失败,您的方法会从回调中返回并继续操作

    return if !verify_recaptcha

    所以你需要把它重写成这样的东西

    def check_captcha
      unless verify_recaptcha
        self.resource = resource_class.new sign_in_params
        respond_with_navigational(resource) do
          flash.discard(:recaptcha_error)
          render :new
        end
      end
    end
    

    【讨论】:

    • 仍然得到相同的不需要的行为 在页面刷新时用户已登录 填写电子邮件和密码字段,不选中 recaptcha,然后单击提交;第一次期望的行为;现在刷新页面,不受欢迎的行为,用户已登录,并且闪烁通知说您已经登录。这真的很疯狂
    • 我的意思不是刷新,我的意思是再次点击提交,用户无需重新验证即可登录
    • 这很奇怪。点击提交后能否附上日志(第一次和第二次)?
    • 过滤器链因 :require_no_authentication 呈现或重定向而停止
    • 我通过将 render :new 更改为 redirect_to new_user_session_path 使其工作,但我也在寻找其他语法
    猜你喜欢
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多