Skip to content

Commit

Permalink
Merge pull request #12 from marcel-strzalka/bugfix/devise
Browse files Browse the repository at this point in the history
Bugfix/devise
  • Loading branch information
marcel-strzalka authored Oct 5, 2022
2 parents 5881308 + 298e834 commit 1c984e2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
29 changes: 29 additions & 0 deletions app/controllers/turbo_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

# This controller is a workaround for a currently (05.10.22) existing bug related to
# Turbo and Devise. Without it flash messages in login and registration forms are
# not displayed at all which is quite confusing to potential users.
#
# Fixed thanks to: https://github.com/hotwired/turbo/issues/45#issuecomment-753370898
#
# TODO: Remove this workaround when the following issue is resolved:
# https://github.com/heartcombo/devise/issues/5446

class TurboController < ApplicationController
class Responder < ActionController::Responder
def to_turbo_stream
controller.render(options.merge(formats: :html))
rescue ActionView::MissingTemplate => e
raise e if get?

if 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
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# frozen_string_literal: true

class User < ApplicationRecord
BCRYPT_BYTESIZE_LIMIT = 72

devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable

has_many :reservations, dependent: :destroy

enum :role, %i[customer manager]

validate do
errors.add(:base, 'Password is too long') if password.bytesize > BCRYPT_BYTESIZE_LIMIT
end
end
26 changes: 19 additions & 7 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 @@ -18,13 +32,13 @@

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

# ==> 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
# with default "from" parameter.
config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'
config.mailer_sender = 'manager@montecinema.com'

# Configure the class responsible to send e-mails.
# config.mailer = 'Devise::Mailer'
Expand Down Expand Up @@ -276,11 +290,9 @@
# ==> 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
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 1c984e2

Please sign in to comment.