heartcombo / devise

Flexible authentication solution for Rails with Warden.
http://blog.plataformatec.com.br/tag/devise/
MIT License
24.02k stars 5.55k forks source link

Default OmniAuth.config.on_failure unexpected behavior #5631

Closed bronzdoc closed 1 year ago

bronzdoc commented 1 year ago

Environment

Current behavior

Is there a way to override the configuration in /lib/devise/omniauth.rb ?

I'm working with an Auth0 password rotation action that gives me an OmniAuth::Strategies::OAuth2::CallbackError error, whenever a user has an expired password, the way to handle this error is defining and on_failure block but with the default on_failure defined in /lib/devise/omniauth.rb I'm getting an unexpected behavior:

Started GET "/auth/auth0/callback?error=access_denied&error_description=Your%20password%20has%20expired.%20%20Please%20reset%20it.&state=eef2801d43f4f57041e67665e0e3959ed8af2980930342f6" for 172.18.0.1 at 2023-09-18 04:02:01 +0000
app  | D, [2023-09-18T04:02:01.837351 #8] DEBUG -- omniauth: (auth0) Callback phase initiated.
app  | E, [2023-09-18T04:02:01.840544 #8] ERROR -- omniauth: (auth0) Authentication failure! access_denied: OmniAuth::Strategies::OAuth2::CallbackError, access_denied | Your password has expired.  Please reset it.
app  | E, [2023-09-18T04:02:01.840810 #8] ERROR -- omniauth: (auth0) Authentication failure! Could not find a valid mapping for path "/auth/auth0/callback": RuntimeError, Could not find a valid mapping for path "/auth/auth0/callback

app  | [app] RuntimeError (Could not find a valid mapping for path "/auth/auth0/callback"):
app  | [app]
app  | [app] devise (4.8.1) lib/devise/mapping.rb:51:in `find_by_path!'
app  | [app] devise (4.8.1) lib/devise/omniauth.rb:16:in `block in <main>'

Error happens here https://github.com/heartcombo/devise/blob/8b0b849a67c46b10827743aa0ccb0679d69e5396/lib/devise/omniauth.rb#L16

I have tried to override it in an initializer with no luck: config/initializers/omniauth.rb

OmniAuth.config.on_failure = Proc.new do |env|
  # Handle error....
end

Expected behavior

Be able to override on_failure in /lib/devise/omniauth.rb

bronzdoc commented 1 year ago

I was able to override it after wrapping the configuration in Rails.application.after_initialize

Rails.application.config.after_initialize do
  OmniAuth.config.on_failure = Proc.new do |env|
    # Handle error....
  end
end