halogenandtoast/oath

See if no_login_redirect can take a block

halogenandtoast opened this issue · 8 comments

We don't have access to the path helper in the config/initializer but perhaps if this option takes a block then we can use those.

The function returns a block that takes the controller, since the path helpers are accessible through that I'd say this issue should be closed.

Hi @halogenandtoast,

I've been trying to set no_login_redirect via a path helper (sign_up_path) without success. Can you help me with an example?

@erikdahlstrand I'm not sure what you're trying to do so before I answer I'd like to get a better idea. From the way you're describing it, it sounds like you want to pass some argument into sign_up_path that will set the no_login_redirect?

Sorry for the weak explanation @halogenandtoast . I'm using the route_translator gem so I want the user to be redirected to the sign in page for the current locale. Therefore I cannot use the default { controller: '/sessions', action: 'new' }, I want the path helper to generate the path based on the current locale. Example /en/login, /sv/logga-in etcetera.

Maybe something like...

Monban.configure do |config|
  config.no_login_redirect = ->(controller) { controller.sign_in_path }
end

I'm passing the locale argument to the helper by default in the Application Controller:

class ApplicationController < ActionController::Base

  private

  def default_url_options(options = {})
    options.merge(locale: I18n.locale)
  end
end

Hmm a couple of things, and I may be wrong, but... I had no problems doing something like this:

https://gist.github.com/halogenandtoast/eff65d5a9df51efadf02fac8ded5a8de

Then if I visited: http://localhost:4040/sv/posts I was redirected to http://localhost:4040/sv/logga-in

But you can't just override I18n.locale and have it go to the correct place in this situation, the route param needs to already be set. If you want it to work with it forcing the locale you can add the following to what I posted above:

https://gist.github.com/halogenandtoast/318126adc179d421e35605db5bcfb663

@erikdahlstrand By this situation above I mean if you got to http://localhost:4040/posts it won't normally send you to http://localhost:4040/sv/logga-in, in order to do that you have to use the second gist I posted.

@halogenandtoast That makes sense. Actually, I'm overriding I18n.locale by trying to set the locale based on browser HTTP Accept-Language, for first time visitors. I should probably also redirect them to the correct path.

Thank you very much for helping me with this. And thank you for an awesome gem.