How to use Devise with Hotwire & Turbo.js Discussion
Hey Chris - If we are still using Webpacker, we just need to change data-turbolinks-track to data-turbo-track in our layout files? In your episode repo, you still have it as turbolinks.
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbo-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbo-track': 'reload' %>
<%= turbo_include_tags %>
<%= stimulus_include_tags %> if we want to use stimulus in webpacker?
Hey Chris, this is super amazing. I'm using hotwire-rails and there's 2 errors that need your expertise:
- The TurboController gives an error
undefined local variable or method
turbo_include_tags'` - When the redirect on incorrect password happens, it doesn't show 'notice'
turbo_include_tags is not necessary. for the second issue, make sure that you have config.navigational_formats = ['*/*', :html, :turbo_stream]
on your devise.rb
I also ran in to this issue. If you could add this line to the source code it would be much better for people trying to follow along.
Your video arrived just in time, thanks !
But I have a blocking issue implementing this solution:
undefined local variable or method
turbo_include_tags'`
I had the same issue. Loading issue concerning application controller.
I put the turbo controller in the controllers dir in turbo_controller.rb and issue was resolved
Also confirmable not longer works when you remove <%= turbo_include_tags %> from the header. And the “turbo_frame_tag” form will no longer show when you run a frame on a page. And you get routing error when you try to log out a session.
No route matches [GET] "/users/sign_out"
I can also confirm that creating a ./app/controllers/turbo_controller.rb
instead of adding it to the top of the devise.rb
initializers fixed my undefined local variable or method 'turbo_include_tags'
error.
Hi Chris, seems you forgot to include the line about config.navigational_formats
in the snippet which was shown in the screencast
Correct me if i'm wrong but the one of disadvantages of this method is that on new session page if user typed wrong email and submits the form the email will not be preserved due to this lines:
if request_format == :turbo_stream
redirect
I found more appropriate solution for me how to fix this.
- do not override
respond
method. - add
data-turbo="false"
attribute to sign in form (which you should copy in advance)
Check this commit for this new feature:
For the login form, now I use Turbo instead of Turbolinks I have 401 response (because of TURBO_STREAM type ?)
Processing by Devise::SessionsController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"username"=>"alex", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms | Allocations: 372)
I'm getting this same error on the login form. The registration form works correctly. I'm using "@hotwired/turbo-rails": "^7.0.0-beta.5",
From what I can tell, Chris has also updated the source code for this episode to use beta.3
. Not sure what I'm doing wrong here.
Thank you Chris! I can't make works errors render, notice works well. Any ideas? 🤔
Take a look at my comment https://gorails.com/forum/how-to-use-devise-with-hotwire-turbo-js-discussion#forum_post_17983
Seems to me a like hacky ... will wait to implement this in a production project, login form handling have to be very basic stuff.
I've got a few devise controllers I've inherited from ( to add some breadcrumbs, etc). They don't like the turbocontroller.
Do I also need to inherit that as well?
Has anyone tried to destroy a user on devise via account edit user page?
I get undefined method `users_url' for #Devise::RegistrationsController:0x0000000000ca58
on line --> redirect_to navigation_location
whenever I try to do so.
I had the same error and I solved with this
config.navigational_formats = ['/', :html, :turbo_stream] in devise.rb
I tried adding the turbo navigational format to devise and unfortunately that did not fix the problem on user destroy for me. What did end up fixing it was modifying the TurboController to match closer to what the responders gem does for other formats.
Look in lib/action_controller/responder.rb#L236-L244 of the responders gem for more context. My fix:
class DeviseTurboController < ApplicationController
class Responder < ActionController::Responder
def to_turbo_stream
if @default_response
@default_response.call(options.merge(formats: :html))
else
controller.render(options.merge(formats: :html))
end
rescue ActionView::MissingTemplate => error
if get?
raise error
elsif has_errors? && default_action
render rendering_options.merge(formats: :html, status: :unprocessable_entity)
else
navigation_behavior error
end
end
end
self.responder = Responder
respond_to :html, :turbo_stream
end
The changes are centered around the default response section. This is where it was getting off the rails on user session destroy for me.
If you're getting a
ActionView::Template::Error (undefined method `turbo_frame_tag' ...
in Production, make sure to set
config.parent_controller = 'Users::DeviseController'
and extract the
class TurboController < ApplicationController
...
end
into app/controllers/users/devise_controller.rb.
See also the hotwire-devise Repo for the source code!
I'm getting an error undefined method
then' for "messages":Stringon my
<%= turbo_stream_from "messages" do %>` line in an app I'm trying to build this into. Any ideas why?
when one logs into to a rails app using devise & turbo. how does the csrf token get updated on the page. I get Can't verify CSRF token authenticity. error when I send a from with turbo after a turbo login
If, like me, you rushed through this video and are now are seeing the error from warden Invalid strategy some_external_strategy
... it could be because you uncommented the entire block in devise.rb
initializer file. I didn't want to post this comment because I might look silly but... in case someone else makes my mistake :D
config.warden do |manager|
manager.failure_app = TurboFailureApp
# manager.intercept_401 = false
# manager.default_strategies(scope: :user).unshift :some_external_strategy
end
and not
config.warden do |manager|
manager.failure_app = TurboFailureApp
manager.intercept_401 = false
manager.default_strategies(scope: :user).unshift :some_external_strategy
end
Hello.
Has anyone managed to use the "current_user" method from Devise in broadcasted partial views?
I have merged the 2 applications "hotwire-devise" & "hotwire-twitter-clone",
and when I add a call to current_user
(like for instance <%= link_to "Edit", edit_tweet_path(tweet) if current_user.present? %> in the _tweet.html.erb file),
I get an error because the method is not available in this context:
Started POST "/tweets" for 127.0.0.1 at 2021-01-29 14:45:53 +0100
Processing by TweetsController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "tweet"=>{"body"=>"TEST"}, "commit"=>"Create Tweet"}
Tweet Create (0.8ms) INSERT INTO "tweets" ("body", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["body", "TEST 003"], ["created_at", "2021-01-29 13:45:53.776383"], ["updated_at", "2021-01-29 13:45:53.776383"]]
Rendered tweets/_tweet.html.erb (Duration: 4.3ms | Allocations: 2000)
Completed 500 Internal Server Error in 30ms (ActiveRecord: 19.6ms | Allocations: 5713)
ActionView::Template::Error (Devise could not find the `Warden::Proxy` instance on your request environment.
Make sure that your application is loading Devise and Warden as expected and that the `Warden::Manager` middleware is present in your middleware stack.
If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the `Devise::Test::ControllerHelpers` module to inject the `request.env['warden']` object for you.):
6: <%= button_to "Likes (#{tweet.likes_count})", tweet_like_path(tweet), method: :post %>
7: <%= button_to "Retweets (#{tweet.retweets_count})", tweet_retweet_path(tweet), method: :post %>
8:
9: <%= link_to "Edit", edit_tweet_path(tweet) if current_user.present? %>
10: </div>
11: </div>
12: <% end %>
DHH replied into that here: https://discuss.hotwire.dev/t/authentication-and-devise-with-broadcasts/1752
Shortly, you can't do that.
consider to separate the TurboController and TurboFailureApp to its own class since put the class inside Devise config could give many much anomalies especially on production because somehow it could ruin your gemfile load order
There's another issue that seems to exist after the migration. When a user tries to change their password the original password always returns as invalid. Has anyone been able to fix this issue?
Warden also throws an error if you have users credentials expire after an inactivity period.
app/channels/application_cable/connection.rb:13:in `find_verified_user'
There was an exception - UncaughtThrowError(uncaught throw :warden)