mikker / passwordless

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

Session create.html.erb view is not rendering in Rails 7 #127

Closed tolesco closed 11 months ago

tolesco commented 1 year ago

Hi there 👋🏻 ,

Seems to be an issue with Rails 7.0.4 , if the user doesn't exist on the database it renders the app/views/passwordless/sessions/create.html.erb view perfectly, but if the user exists on the database, nothing is shown on the view.

I suspect the issue comes from the new TURBO_STREAM format, thoughts?

Evidence:

2022-10-16 17 37 32

I'm receiving the magic link:

Full request log:

Started POST "/users/sign_in" for 127.0.0.1 at 2022-10-16 17:37:47 -0500
Processing by Passwordless::SessionsController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "passwordless"=>"[FILTERED]", "commit"=>"Send magic link", "authenticatable"=>"user"}
  Store Load (0.3ms)  SELECT "stores".* FROM "stores" ORDER BY "stores"."id" ASC LIMIT $1  [["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:12:in `set_store'
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE (lower(email) = 'francisco@tolesco.io') ORDER BY "users"."id" ASC LIMIT $1  [["LIMIT", 1]]
  TRANSACTION (0.1ms)  BEGIN
  Passwordless::Session Load (0.4ms)  SELECT "passwordless_sessions".* FROM "passwordless_sessions" WHERE "passwordless_sessions"."token" = $1 LIMIT $2  [["token", "[FILTERED]"], ["LIMIT", 1]]
  Passwordless::Session Create (1.3ms)  INSERT INTO "passwordless_sessions" ("authenticatable_type", "authenticatable_id", "timeout_at", "expires_at", "claimed_at", "user_agent", "remote_addr", "token", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"  [["authenticatable_type", "User"], ["authenticatable_id", "ec7974d3-f523-403e-b23e-011b1628db79"], ["timeout_at", "2022-10-16 22:52:47.301462"], ["expires_at", "2023-10-16 22:37:47.301364"], ["claimed_at", nil], ["user_agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:105.0) Gecko/20100101 Firefox/105.0"], ["remote_addr", "127.0.0.1"], ["token", "[FILTERED]"], ["created_at", "2022-10-16 22:37:47.304734"], ["updated_at", "2022-10-16 22:37:47.304734"]]
  TRANSACTION (0.4ms)  COMMIT
  Rendering /Users/tolesco/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/passwordless-0.11.0/app/views/passwordless/mailer/magic_link.text.erb
  Rendered /Users/tolesco/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/passwordless-0.11.0/app/views/passwordless/mailer/magic_link.text.erb (Duration: 0.2ms | Allocations: 47)
Passwordless::Mailer#magic_link: processed outbound mail in 2.2ms
Delivered mail 634c87bb4cc05_76fc3bd8630d5@Jesuss-MacBook-Pro.local.mail (542.7ms)
Date: Sun, 16 Oct 2022 17:37:47 -0500
From: no-reply@tolesco.io
To: francisco@tolesco.io
Message-ID: <634c87bb4cc05_76fc3bd8630d5@Jesuss-MacBook-Pro.local.mail>
Subject: =?UTF-8?Q?Your_magic_link_=E2=9C=A8?=
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Here's your link: http://localhost:3000/users/sign_in/ES_O-2dVuNWv0T9ihEsdfhXnFHcp5u4J_S6_egKtIvw

  Rendering layout layouts/application.html.erb
  Rendering /Users/tolesco/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/passwordless-0.11.0/app/views/passwordless/sessions/create.html.erb within layouts/application
  Rendered /Users/tolesco/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/passwordless-0.11.0/app/views/passwordless/sessions/create.html.erb within layouts/application (Duration: 0.1ms | Allocations: 31)
  Rendered shared/_flash_messages.html.erb (Duration: 0.1ms | Allocations: 37)
  Rendered layout layouts/application.html.erb (Duration: 4.5ms | Allocations: 2299)
Completed 200 OK in 567ms (Views: 5.2ms | ActiveRecord: 2.8ms | Allocations: 13542)
tolesco commented 1 year ago

I just saw that this is already being attended here: Always redirect magic link requests back to the sign_in page and render generic flash

yshmarov commented 1 year ago

I just generated the views with rails generate passwordless:views, and sessions/create was created as sessions.create..

Screenshot 2022-11-07 at 22 38 39
yshmarov commented 1 year ago

@tolesco try adding , data: { turbo: 'false' } - it helped me with the redirect after form submission

-<%= form_with model: @session, url: send(Passwordless.mounted_as).sign_in_path do |f| %>
+<%= form_with model: @session, url: send(Passwordless.mounted_as).sign_in_path, data: { turbo: 'false' } do |f| %>
tolesco commented 1 year ago

Thank you @yshmarov !, works great!

mikker commented 11 months ago

Short-term solution is to use data: { turbo: false } like @yshmarov suggests above. (Thank you!)

Passwordless 1.0 handles this more gracefully.