Rails 7 + Devise + Turbo stream not showing errors when sign in

1.9k Views Asked by At

I started new project and am stuck for hours trying to make Sign In page show errors. Though when I try to write incorrect data on Sign up page it returns errors, Sign in completely ignores it. I found decision concerning devise for rails 7 from gorails, it didn't help.

Gemfile:

gem "devise", git: "https://github.com/ghiculescu/devise.git", branch: "error-code-422"
gem "responders", git: "https://github.com/heartcombo/responders.git"

config/initializers/devise.rb:

Devise.setup do |config|
...
config.navigational_formats = ['*/*', :html, :turbo_stream]
...

Did it in order not to add new controller and custom error as it was shown in video. Also, console returns 422, but in terminal still:

Started POST "/users/sign_in" for ::1 at 2022-11-29 12:46:57 +0200
Processing by Devise::SessionsController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
Completed 401 Unauthorized in 271ms (ActiveRecord: 0.3ms | Allocations: 1727)


Processing by Devise::SessionsController#new as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
  Rendering layout layouts/application.html.haml
  Rendering devise/sessions/new.html.haml within layouts/application
  Rendered devise/shared/_error_messages.html.haml (Duration: 0.0ms | Allocations: 12)
  Rendered devise/shared/_links.html.haml (Duration: 0.2ms | Allocations: 137)
  Rendered devise/sessions/new.html.haml within layouts/application (Duration: 3.5ms | Allocations: 1348)
  Rendered layout layouts/application.html.haml (Duration: 5.4ms | Allocations: 2506)
Completed 200 OK in 251ms (Views: 6.3ms | ActiveRecord: 0.0ms | Allocations: 3124)

My views are default generated by devise. If I should provide more info, like application.html.haml etc, please tell me.

1

There are 1 best solutions below

0
On

Edit: Devise 4.9.0 has been released, which fixes this issue. Upgrade Devise and modify your Devise config to match the block below. Configs generated after 4.9.0 will have the correct values.

From: https://github.com/heartcombo/devise/blob/v4.9.0/CHANGELOG.md:

Devise uses the latest responders version (v3.1.0 or higher), which allows configuring the status used for validation error responses (error_status) and for redirects after POST/PUT/PATCH/DELETE requests (redirect_status). For backwards compatibility, Devise keeps error_status as :ok which returns a 200 OK response, and redirect_status to :found which returns a 302 Found response, but you can configure it to return 422 Unprocessable Entity and 303 See Other respectively, to match the behavior expected by Hotwire/Turbo:

# config/initializers/devise.rb
Devise.setup do |config|
  # ...
  config.responder.error_status = :unprocessable_entity
  config.responder.redirect_status = :see_other
  # ...
end

Original answer / workaround (no longer needed):

This is a known issue with Devise, and for now the recommended fix is to disable Turbo on Devise's log in/out forms.

From https://github.com/heartcombo/devise/wiki/Troubleshooting-Rails-7-and-Turbo-Drive :

Since Turbolinks is replaced with Turbo Drive (That is part of Hotwire), we need to ensure full-page redirects when submitting devise forms.

One trick you can use for Devise's log out link, since it uses the HTTP DELETE method, is to change it to a button, like so:

<%= button_to "Log out", destroy_user_session_path, method: :delete, data: { turbo: false } %>

... but the wiki link above has other samples as well.

There are some more details and discussions here: