简体   繁体   English

Rails检查Devise是否有任何错误?

[英]Rails check if Devise has any errors?

I am looking for a way to check if Devise has any errors (invalid credentials etc.) for a before_action method in my ApplicationController. 我正在寻找一种方法来检查Devise是否对我的ApplicationController中的before_action方法有任何错误(无效的凭据等)。 There is code in that method that I need only to run if Devise has no errors. 该方法中的代码仅在Devise没有错误时才需要运行。

class ApplicationController < ActionController::Base
   before_action :foo

   def foo
      if !devise_errors?
   end
end

Do you mean sign in errors? 您的意思是登录错误吗? Wouldn't you only need this in the session controller? 您不是仅在会话控制器中需要它吗?

You could check the flash messages... 您可以检查Flash消息...

But you might be better off checking in Warden : 但是您最好检查一下Warden

Warden::Manager.warden_callback do |user, auth, opts|
  # code 
end

You can check credential errors like this: 您可以检查凭证错误,如下所示:

class ApplicationController < ActionController::Base
  before_action :foo

  def foo
    if !devise_errors?
  end
  ..
  private

  def devise_errors?
    login_params = devise_parameter_sanitizer.sanitize(:sign_in)
    email = login_params.dig(:email)
    password = login_params.dig(:password)
    user = User.find_by(email: email)
    return true if user.blank?

    !user.valid_password?(password)
  end
  ..
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM