Skip to content
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

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

Closed
andreaskundig opened this issue Jan 31, 2024 · 4 comments
Closed

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

andreaskundig opened this issue Jan 31, 2024 · 4 comments

Comments

@andreaskundig
Copy link
Contributor

andreaskundig commented Jan 31, 2024

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 andreaskundig changed the title How can I configure locale in scope? How can I configure a locale in the url like /en/users/sign_in? Jan 31, 2024
@andreaskundig andreaskundig changed the title How can I configure a locale in the url like /en/users/sign_in? Configuring a locale in the url: /en/users/sign_in Jan 31, 2024
@andreaskundig
Copy link
Contributor Author

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 `url_for'

passwordless/mailer.rb:16:in `sign_in'

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
Copy link
Owner

mikker commented Feb 1, 2024

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
Copy link
Owner

mikker commented Feb 1, 2024

@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
Copy link
Owner

mikker commented Feb 1, 2024

I've created a feature request issue in #206

@mikker mikker closed this as completed Feb 1, 2024
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

2 participants