Skip to content

Commit

Permalink
Hacer compatible Devise con Turbo Fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinjosmell committed Sep 15, 2022
1 parent 05749d0 commit 25c15b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/controllers/turbo_devise_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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
19 changes: 19 additions & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# frozen_string_literal: true

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

# Assuming you have not yet modified this file, each configuration option below
# is set to its default value. Note that some are commented out while others
# are not: uncommented lines are intended to protect your configuration from
Expand All @@ -19,6 +33,7 @@
# ==> Controller configuration
# Configure the parent class to the devise controllers.
# config.parent_controller = 'DeviseController'
config.parent_controller = 'TurboDeviseController'

# ==> Mailer Configuration
# Configure the e-mail address which will be shown in Devise::Mailer,
Expand Down Expand Up @@ -264,6 +279,7 @@
#
# The "*/*" below is required to match Internet Explorer requests.
# config.navigational_formats = ['*/*', :html]
config.navigational_formats = ['*/*', :html, :turbo_stream]

# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :delete
Expand All @@ -281,6 +297,9 @@
# manager.intercept_401 = false
# manager.default_strategies(scope: :user).unshift :some_external_strategy
# end
config.warden do |manager|
manager.failure_app = TurboFailureApp
end

# ==> Mountable engine configurations
# When using Devise inside an engine, let's call it `MyEngine`, and this engine
Expand Down

0 comments on commit 25c15b8

Please sign in to comment.