Skip to content

UrlGenerationError for new_session_path (Custom authentication via Rails 8) #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
stebo opened this issue Feb 17, 2025 · 4 comments
Open

Comments

@stebo
Copy link

stebo commented Feb 17, 2025

Issue

When using custom authentication via Rails 8 new Authentication generator, opening .../jobs, which should require a logged-in user or redirect to login page, results in a UrlGenerationError:

ActionController::UrlGenerationError in MissionControl::Jobs::QueuesController#index
No route matches {action: "new", controller: "sessions", server_id: nil}
def request_authentication
  session[:return_to_after_authenticating] = request.url
  redirect_to new_session_path
end

This would make any kind of custom authentication with redirects difficult and should not only effect the Rails 8 Auth generator.

To replicate

  • init new Rails 8 project
  • add gem "mission_control-jobs" and mount to "/jobs" according to Readme
  • run bin/rails generate authentication (which includes Authentication for all controllers)
  • run bin/dev
  • go to http://127.0.0.1:3000/jobs
  • -> UrlGenerationError

Quick Fix

Adding include Rails.application.routes.url_helpers to the MissionControl::Jobs::QueuesController solves the problem, but does not feel like a proper fix.

Weirdly, adding the url_helpers to the MissionControl::Jobs::ApplicationController does not help.

--

Any help or suggestions appreciated.

@eddyjaga
Copy link

eddyjaga commented Feb 21, 2025

I resolved the issue by creating a BaseController and pointed to the controller in application.rb like this.

# Use BaseController for MissionControl jobs
config.mission_control.jobs.base_controller_class = "BaseController"

This is optional, avoid authentication in dev env.

# Disable HTTP basic auth for MissionControl jobs
 config.mission_control.jobs.http_basic_auth_enabled = false
Image

@stebo
Copy link
Author

stebo commented Feb 22, 2025

I resolved the issue by creating a BaseController and pointed to the controller in application.rb like this.

But what type of Authorization did you set in your BaseController? I assume you set allow_unauthenticated_access, which then does NOT trigger a redirect, and hence it would work. But if you want to enforce a user session and redirect to login, the above mentioned UrlGenerationError comes up...

@fdocr
Copy link

fdocr commented Mar 10, 2025

I'm running into the same/similar issue. Routes aren't picked up by the Mission Control engine. I'm also using Rails Authentication generator and configured a custom AdminController:

class AdminController < ApplicationController
  before_action :authenticate_admin!
end
# config/application.rb

config.mission_control.jobs.base_controller_class = "AdminController"
config.mission_control.jobs.http_basic_auth_enabled = false
# ApplicationController method

def authenticate_admin!
  binding.break
  redirect_to root_path, alert: "You are not authorized to access this page" unless Current.user.admin?
end

This is what I see in the breakpoint:

Image

This is what I would expect them to be (works on other controllers that use the same before_action :authenticate_admin!):

Image

This means the before_action filter redirects to /jobs/, which will "infinite loop" error out. A quick fix was to replace root_path with "/" but also ran into some issues with I18n translations not being found.

The solution mentioned in #183 didn't work for me even though it feels somewhat related. I'll revisit this at another time, but wonder what might be wrong with this setup.

@aarisr
Copy link

aarisr commented Mar 12, 2025

I was able to overcome this by replacing the new_session_path call with an explicit call of Rails.application.routes.url_helpers.new_session_path but that is a similar fix to what @stebo proposed with including the module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants