Skip to content

Commit

Permalink
TurboDevisecontroller, TurboFailureApp setup to manage devise respons…
Browse files Browse the repository at this point in the history
…e formats
  • Loading branch information
developerJai committed Oct 17, 2022
1 parent c131432 commit bf5009e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 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
41 changes: 28 additions & 13 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
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
# frozen_string_literal: true

# Assuming you have not yet modified this file, each configuration option below
Expand All @@ -16,10 +29,6 @@
# by default. You can change it below and use your own secret key.
# config.secret_key = 'e9ccc57c083237e851acbec31f23c1b48225e28970072b37dde0bb5d19c897f7713ddf1424f7bb1401ce6bd572cb968bb05d875b23453982c91e62dee42bf05b'

# ==> Controller configuration
# Configure the parent class to the devise controllers.
# config.parent_controller = 'DeviseController'

# ==> Mailer Configuration
# Configure the e-mail address which will be shown in Devise::Mailer,
# note that it will be overwritten if you use your own mailer class
Expand Down Expand Up @@ -273,15 +282,6 @@
# up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'

# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
# change the failure app, you can configure them inside the config.warden block.
#
# config.warden do |manager|
# manager.intercept_401 = false
# manager.default_strategies(scope: :user).unshift :some_external_strategy
# end

# ==> Mountable engine configurations
# When using Devise inside an engine, let's call it `MyEngine`, and this engine
# is mountable, there are some extra configurations to be taken into account.
Expand All @@ -308,6 +308,21 @@
# When set to false, does not sign a user in automatically after their password is
# changed. Defaults to true, so a user is signed in automatically after changing a password.
# config.sign_in_after_change_password = true

# Turbo stream configuration for navigation formats
config.navigational_formats = ['*/*', :html, :turbo_stream]

# ==> Controller configuration
# Configure the parent class to the devise controllers.
config.parent_controller = 'TurboDeviseController'

# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
# change the failure app, you can configure them inside the config.warden block.
config.warden do |manager|
# manager.intercept_401 = false
# manager.default_strategies(scope: :user).unshift :some_external_strategy
manager.failure_app = TurboFailureApp
end

end

0 comments on commit bf5009e

Please sign in to comment.