ruby-on-railsdevisewarden

Access user in Warden before_failure callback


Is there a way to access the user that failed authentication via the before_failure callback?

Warden::Manager.before_failure do |env, opts|
  request = Rack::Request.new(env)
  env['SCRIPT_INFO'] =~ /\/(.*)/
  request.params[:action] = $1
end

I am placing this configuration in the devise initializer, I have heard other people mention placing it in the user model so maybe I can access it that way.


Solution

  • The suggestion in the first comment above is a viable solution and the user email is available.

    I was able to verify that the answer was correct but stumbling upon another answer here: https://stackoverflow.com/a/33230548/1945948

    The email is available on the before_failure callback via:

    email = env["action_dispatch.request.request_parameters"][:user] &&
            env["action_dispatch.request.request_parameters"][:user][:email]
    

    and then the email can be used to find your user.