Skip to content

Instantly share code, notes, and snippets.

@Deanout
Created January 24, 2022 11:39
Show Gist options
  • Save Deanout/86e17d99ff4807c3a077674496e6a489 to your computer and use it in GitHub Desktop.
Save Deanout/86e17d99ff4807c3a077674496e6a489 to your computer and use it in GitHub Desktop.
bundle add devise
bundle install
rails g devise:install
class TurboDeviseController < ApplicationController
class Responder < ActionController::Responder
def to_turbo_stream
controller.render(options.merge(formats: :html))
rescue ActionView::MissingTemplate => error
if get?
raise error
elsif has_errors? && default_action
render rendering_options.merge(formats: :html, status: :unprocessable_entity)
else
redirect_to navigation_location
end
end
end
self.responder = Responder
respond_to :html, :turbo_stream
end
# /config/initializers/devise.rb
# Turbo doesn't work with devise by default.
# Keep tabs on https://github.com/heartcombo/devise/issues/5446 for a possible fix
# Fix from https://gorails.com/episodes/devise-hotwire-turbo
class TurboFailureApp < Devise::FailureApp
def respond
if request_format == :turbo_stream
redirect
else
super
end
end
def skip_format?
%w(html turbo_stream */*).include? request_format.to_s
end
end
# ...
Devise.setup do |config|
# ...
# ==> Controller configuration
# Configure the parent class to the devise controllers.
config.parent_controller = 'TurboDeviseController'
# ...
# ==> Navigation configuration
# ...
config.navigational_formats = ['*/*', :html, :turbo_stream]
# ...
# ==> Warden configuration
# ...
config.warden do |manager|
manager.failure_app = TurboFailureApp
# manager.intercept_401 = false
# manager.default_strategies(scope: :user).unshift :some_external_strategy
end
# ...
end
# app/views/layouts/application.html.erb, above the <%= yield %>
<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert alert-danger"><%= alert %></p>
<% end %>
# config/routes.rb
Rails.application.routes.draw do
devise_scope :user do
# Redirests signing out users back to sign-in
get "users", to: "devise/sessions#new"
end
devise_for :users
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment