如何获得在AJAX中使用的未确认或锁定状态的用户帐户

How to get Devise user account unconfirmed or locked state to use in AJAX?

本文关键字:锁定 状态 用户 确认 AJAX 何获得      更新时间:2023-09-26

我试图把设计视图使用JS来处理响应。我想使用默认的设计错误消息,但我不能得到个别类型的错误(例如:未经确认;锁定的帐户),因为监狱长。认证。所以,我使用"caught",所以它不会抛出406或其他错误。我的问题是:我知道"caught[:message] ==:unconfirmed"表示用户的"未确认"状态,那么"locked"对应的符号应该是什么呢?:locked不工作,我找不到文档。

我有我的Sessions_controller这样:def创建catch = catch(:典狱长)do自我。资源=监狱长。Authenticate:scope => resource_name

结束
    if resource 
      # User is confirmed
      sign_in(resource_name, resource)
      puts "LOGGED IN!!!"
      respond_to js{
          set_flash_message(:success, :signed_in)
          render :template => "remote_content/flashes.js.erb"
          flash.discard
        }
    elsif caught and caught[:message] == :unconfirmed
      # User is unconfirmed
      puts "UNCONFIRMED ACCOUNT!!!"
      # send the email or display the flash with link to send email
      respond_to js{
          set_flash_message(:error, :problem) #:problem is in devise.en.yml "There is problem in your account, check you email."
          render :template => "remote_content/form_flashes.js.erb"
          flash.discard
        }
    else
      # User is not signed in, should be... error in credentials or locked account....
      puts "ERROR IN CREDENTIALS!!!"
      respond_to js{
          set_flash_message(:error, :invalid)
          render :template => "remote_content/form_flashes.js.erb"
          flash.discard
        }
    end
  end

flashes.js.erb/form_flashes.js。动词执行得很好,没问题!就像这样:

$('.modal').modal('hide');
// append flash to the body
$('.body').append("<%= escape_javascript raw(flash_normal) %>");

你觉得我的方法怎么样?我应该使用CustomFailure代替吗?我找不到任何CustomFailure的例子,或设计的原始,所以我可以把它响应我的JS文件。

我发现通过检查caught,唯一的消息是unconfirmed,它从未捕获unlocked状态。所以我不得不通过电子邮件获得User,并使用设计助手access_locked?来获取他的信息。我知道这可能是一个安全故障,但如果锁上了,你什么也做不了。下面是上面缺少的那部分代码:

  ...
  else
  ## User is not signed in, should be... error in credentials or locked...
  ## Let's see if it's locked
  # This function is only used like this (without security)
  # only when the authentication already failed in that action
  # but we still need to get the user in order to check if its locked
  user = User.find_by email: params[:user][:email]
  if !user.nil? and user.access_locked?
    # The small issue: anyone can know that an email is locked without typing the password
    puts "ACCOUNT LOCKED!!!"
    respond_to js{
        set_flash_message(:error, :locked)
        render :template => "remote_content/form_flashes.js.erb"
        flash.discard
      }
  else
    ## If it's not Locked, then it's error in credentials
    puts "ERROR IN CREDENTIALS!!!"
    respond_to js{
        set_flash_message(:error, :invalid)
        render :template => "remote_content/form_flashes.js.erb"
        flash.discard
      }
  end
end

我知道这不是最漂亮的方法,但是很有效。