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

Downcase authentication keys and humanize error message #4834

Merged
merged 3 commits into from
Jan 2, 2019

Conversation

grantzau
Copy link

@grantzau grantzau commented Apr 5, 2018

To fix #4095

@@ -103,11 +103,11 @@ def i18n_message(default = nil)
options[:scope] = "devise.failure"
options[:default] = [message]
auth_keys = scope_class.authentication_keys
keys = (auth_keys.respond_to?(:keys) ? auth_keys.keys : auth_keys).map { |key| scope_class.human_attribute_name(key) }
keys = (auth_keys.respond_to?(:keys) ? auth_keys.keys : auth_keys).map { |key| scope_class.human_attribute_name(key).downcase }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we add tests for it? 🤔

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, created the pull request a bit to early!

Copy link
Author

@grantzau grantzau Apr 5, 2018

Choose a reason for hiding this comment

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

Edited the existing tests. They check, that the downcasing are done. There's not really a test to check the added humanize method though, not sure, if that's necessary?

Copy link
Collaborator

@feliperenan feliperenan Apr 6, 2018

Choose a reason for hiding this comment

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

I would add tests to ensure that it's the expected behavior.

@grantzau grantzau changed the title Grammatically correct login error message Downcase authentication keys and humanize error message Apr 5, 2018
@feliperenan
Copy link
Collaborator

feliperenan commented Apr 6, 2018

@grantzau What do you think about squash the commits? All of them is about the same change - downcase authentication keys and humanize error message, so for me, just one commit is enough.

Here a great blog post about it: https://blog.carbonfive.com/2017/08/28/always-squash-and-rebase-your-git-commits/

@grantzau
Copy link
Author

grantzau commented Apr 6, 2018 via email

@grantzau grantzau force-pushed the patch-1 branch 2 times, most recently from d9372e4 to cca5a7b Compare April 8, 2018 18:43
@grantzau
Copy link
Author

grantzau commented Apr 8, 2018

Added tests and rebased commits.

test 'uses custom i18n options' do
call_failure('warden' => OpenStruct.new(message: :does_not_exist), app: FailureWithI18nOptions)
assert_equal 'User Steve does not exist', @request.flash[:alert]
assert_equal 'User steve does not exist', @request.flash[:alert]
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this one, I think it should have stayed as it was before. This is a custom message defined here: https://github.com/plataformatec/devise/blob/88724e10adaf9ffd1d8dbfbaadda2b9d40de756a/test/support/locale/en.yml#L5 and we're passing the user's name Steve in a capitalized form here: https://github.com/grantzau/devise/blob/cca5a7b8e9f699de4299e001d95e3e269470f47f/test/failure_app_test.rb#L27, which gets lost when we call #humanize

[3] pry(main)> "User Steve does not exist".humanize
=> "User steve does not exist"

I'm not sure what's the best solution here. Maybe we should only call #humanize when the message is :invalid 🤔

@smoyte
Copy link

smoyte commented Dec 1, 2018

Note that you can control i18n of parameters to failure messages by doing something like this:

In your models:

module People
  module Users
    # Overriding the i18n_options method in the Devise FailureApp so that we can have control
    # over how the `authentication_keys` parameter to the messages under devise.failure is translated.
    # By default the `human_attribute_name` of the attribute is used, but those are typically capitalized,
    # while the messages call for a lowercase value.
    # This custom failure app must be activated in the config/initializers/devise.rb file.
    class CustomDeviseFailureApp < Devise::FailureApp
      def i18n_options(options)
        options.merge(authentication_keys: I18n.t("devise.authentication_keys.email"))
      end
    end
  end
end

and then in config/initializers/devise.rb:

  config.warden do |manager|
    manager.failure_app = People::Users::CustomDeviseFailureApp
  end

and then in your devise.yml file:

  devise:
    ...
    authentication_keys:
      email: "email"

Ideally Devise would support an optional authentication_keys key in the YML file out of the box, so you could have control over these params without a custom failure app, but this works right now and is not particularly hacky. There is support for this method in the tests even, see here.

Hope this helps!

@tegon
Copy link
Member

tegon commented Dec 27, 2018

@grantzau are you still going to work on this?

I'm asking to know if I should assign it to someone else.

@tegon tegon added this to the 5.0 milestone Jan 2, 2019
@tegon tegon changed the base branch from master to 5-rc January 2, 2019 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants