Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/devise #12

Merged
merged 3 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would store this email in env 🤔

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll leave that as it is for now and gonna get back to it when I prepare my app for deployment.


# 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