mikker / passwordless

🗝 Authentication for your Rails app without the icky-ness of passwords
MIT License
1.29k stars 88 forks source link

Configuring a locale in the url: /en/users/sign_in #205

Closed andreaskundig closed 9 months ago

andreaskundig commented 9 months ago

I would like to use urls that include the locale, like /en/users/sign_in like documented in the rails i18n doc

I tried setting the scope like this:

  scope ":locale" do
    passwordless_for :users

/en/users/sign_in works, but when I enter an email and click on the button, I get

ActionController::UrlGenerationError in Passwordless::SessionsController#create

No route matches {:action=>"confirm", :authenticatable=>:user, :controller=>"passwordless/sessions", :id=>"57137bd7-2ff0-454c-a945-7a1d15a07758", :resource=>:users, :token=>"ALZJD1"}

The error happens in I18n.with_locale, in the switch_locale function that I copied from the i18n rails doc

  def switch_locale(&action)
    locale = params[:locale] || I18n.default_locale
    I18n.with_locale(locale, &action)
  end

Is there a way to get this to work?

andreaskundig commented 9 months ago

In the stacktrace, with_locale goes through two passwordless methods.

mailer.sign_in tries to build a magic_link with context.url_for and fails

_passwordless/context.rb:42:in `urlfor'

_passwordless/mailer.rb:16:in `signin'

actionpack (7.1.2) lib/action_dispatch/journey/formatter.rb:44:in `path'
actionpack (7.1.2) lib/action_dispatch/routing/route_set.rb:854:in `url_for'
actionpack (7.1.2) lib/action_dispatch/routing/url_for.rb:184:in `full_url_for'
actionpack (7.1.2) lib/action_dispatch/routing/url_for.rb:174:in `url_for'
actionpack (7.1.2) lib/action_dispatch/routing/route_set.rb:537:in `url_for'
passwordless (1.2.0) lib/passwordless/context.rb:42:in `url_for'
passwordless (1.2.0) app/mailers/passwordless/mailer.rb:16:in `sign_in'
actionpack (7.1.2) lib/abstract_controller/base.rb:224:in `process_action'
actionpack (7.1.2) lib/abstract_controller/callbacks.rb:259:in `block in process_action'
activesupport (7.1.2) lib/active_support/callbacks.rb:121:in `block in run_callbacks'
actiontext (7.1.2) lib/action_text/rendering.rb:23:in `with_renderer'
actiontext (7.1.2) lib/action_text/engine.rb:69:in `block (4 levels) in <class:Engine>'
activesupport (7.1.2) lib/active_support/callbacks.rb:130:in `instance_exec'
activesupport (7.1.2) lib/active_support/callbacks.rb:130:in `block in run_callbacks'
activesupport (7.1.2) lib/active_support/callbacks.rb:141:in `run_callbacks'
actionpack (7.1.2) lib/abstract_controller/callbacks.rb:258:in `process_action'
actionpack (7.1.2) lib/abstract_controller/base.rb:160:in `process'
actionmailer (7.1.2) lib/action_mailer/rescuable.rb:29:in `block in process'
actionmailer (7.1.2) lib/action_mailer/rescuable.rb:21:in `handle_exceptions'
actionmailer (7.1.2) lib/action_mailer/rescuable.rb:28:in `process'
actionview (7.1.2) lib/action_view/rendering.rb:40:in `process'
actionmailer (7.1.2) lib/action_mailer/base.rb:658:in `block in process'
activesupport (7.1.2) lib/active_support/notifications.rb:206:in `block in instrument'
activesupport (7.1.2) lib/active_support/notifications/instrumenter.rb:58:in `instrument'
activesupport (7.1.2) lib/active_support/notifications.rb:206:in `instrument'
actionmailer (7.1.2) lib/action_mailer/base.rb:657:in `process'
actionmailer (7.1.2) lib/action_mailer/message_delivery.rb:136:in `block in processed_mailer'
<internal:kernel>:90:in `tap'
actionmailer (7.1.2) lib/action_mailer/message_delivery.rb:135:in `processed_mailer'
actionmailer (7.1.2) lib/action_mailer/message_delivery.rb:124:in `deliver_now'
passwordless (1.2.0) lib/passwordless/config.rb:47:in `block in <class:Configuration>'
passwordless (1.2.0) app/controllers/passwordless/sessions_controller.rb:34:in `create'
actionpack (7.1.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (7.1.2) lib/abstract_controller/base.rb:224:in `process_action'
actionpack (7.1.2) lib/action_controller/metal/rendering.rb:165:in `process_action'
actionpack (7.1.2) lib/abstract_controller/callbacks.rb:259:in `block in process_action'
activesupport (7.1.2) lib/active_support/callbacks.rb:121:in `block in run_callbacks'
i18n (1.14.1) lib/i18n.rb:322:in `with_locale' 
mikker commented 9 months ago

Yes, I believe this is due to how passwordless builds its urls. I'm afraid we don't support scoped urls like that currently.

It is not something I have the bandwidth to add support for atm, but I'll gladly review and possibly include a nice PR if someone is up for the challenge.

mikker commented 9 months ago

@andreaskundig If you need this url structure, you can set up your own routes and possibly controllers/mailers/routes. Use the built-in ones as a starting point. Will take a bit of work but it's possible.

mikker commented 9 months ago

I've created a feature request issue in #206