Devise log after auth failure

后端 未结 6 2138
梦谈多话
梦谈多话 2020-12-09 00:07

I need to write a log when somebody failes to log in to my app (to track bruteforce attempts). Also I decided to log successful authentications. So I created a Sess

6条回答
  •  醉话见心
    2020-12-09 00:29

    Prakash's answer is helpful, but it's not ideal to rely on SessionsController#new to be run as a side effect. I believe this is cleaner:

    class LogAuthenticationFailure < Devise::FailureApp
      def respond
        if request.env.dig('warden.options', :action) == 'unauthenticated'
          Rails.logger.info('...')
        end
        super
      end
    end
    
    ...
    
    Devise.setup do |config|
    
    config.warden do |manager|
      manager.failure_app = LogAuthenticationFailure
    end
    

    Check out Graeme's answer if you'd prefer to hook into Warden's callbacks (Devise is implemented using Warden).

提交回复
热议问题