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

Feature/customable authorized users only error response #869

17 changes: 8 additions & 9 deletions lib/devise_token_auth/controllers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module ClassMethods
# blogger_signed_in? # Checks whether there is either a user or an admin signed in
# current_blogger # Currently signed in user or admin
# current_bloggers # Currently signed in user and admin
# render_authenticate_blogger_error # Render error unless user or admin are signed in
# render_authenticate_error # Render error unless user or admin are signed in
#
# Use:
# before_action :authenticate_blogger! # Redirects unless either a user or an admin are authenticated
Expand All @@ -39,7 +39,7 @@ def authenticate_#{group_name}!(favourite=nil, opts={})
end

unless current_#{group_name}
render_authenticate_#{group_name}_error
render_authenticate_error
end
end
end
Expand All @@ -66,14 +66,14 @@ def current_#{group_name.to_s.pluralize}
end.compact
end

def render_authenticate_#{group_name}_error
def render_authenticate_error
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed the method name to render_authenticate_error .

Exists error-render methods are using simple method name like render_create_error.
They don't include #{group_name} and #{mapping} like render_create_error.
So new method name also don't include these.

https://github.com/lynndylanhurley/devise_token_auth#registrations-controller

return render json: {
errors: [I18n.t('devise.failure.unauthenticated')]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have resolved conflict about error message.

}, status: 401
end

if respond_to?(:helper_method)
helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?", "render_authenticate_#{group_name}_error"
helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?", "render_authenticate_error"
end
METHODS
end
Expand Down Expand Up @@ -103,8 +103,7 @@ def log_process_action(payload)
# current_admin # Current signed in admin
# user_session # Session data available only to the user scope
# admin_session # Session data available only to the admin scope
# render_authenticate_user_error # Render error unless user is signed in
# render_authenticate_admin_error # Render error unless admin is signed in
# render_authenticate_error # Render error unless user or admin is signed in
#
# Use:
# before_action :authenticate_user! # Tell devise to use :user map
Expand All @@ -116,7 +115,7 @@ def self.define_helpers(mapping) #:nodoc:
class_eval <<-METHODS, __FILE__, __LINE__ + 1
def authenticate_#{mapping}!
unless current_#{mapping}
render_authenticate_#{mapping}_error
render_authenticate_error
end
end

Expand All @@ -132,7 +131,7 @@ def #{mapping}_session
current_#{mapping} && warden.session(:#{mapping})
end

def render_authenticate_#{mapping}_error
def render_authenticate_error
return render json: {
errors: [I18n.t('devise.failure.unauthenticated')]
}, status: 401
Expand All @@ -141,7 +140,7 @@ def render_authenticate_#{mapping}_error

ActiveSupport.on_load(:action_controller) do
if respond_to?(:helper_method)
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session", "render_authenticate_#{mapping}_error"
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session", "render_authenticate_error"
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/demo_group_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest
end

it 'should define render_authenticate_user_error' do
assert @controller.methods.include?(:render_authenticate_user_error)
assert @controller.methods.include?(:render_authenticate_error)
end

it 'should define render_authenticate_mang_error' do
assert @controller.methods.include?(:render_authenticate_mang_error)
assert @controller.methods.include?(:render_authenticate_error)
end

it 'should define render_authenticate_member_error' do
assert @controller.methods.include?(:render_authenticate_member_error)
assert @controller.methods.include?(:render_authenticate_error)
Copy link
Contributor

Choose a reason for hiding this comment

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

These three tests now make less sense: their it descriptions talk about method names that are not checked for existence.

end
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/demo_mang_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest
end

it 'should define render_authenticate_user_error' do
assert @controller.methods.include?(:render_authenticate_user_error)
assert @controller.methods.include?(:render_authenticate_error)
end

it 'should define render_authenticate_mang_error' do
assert @controller.methods.include?(:render_authenticate_mang_error)
assert @controller.methods.include?(:render_authenticate_error)
Copy link
Contributor

Choose a reason for hiding this comment

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

Also here: The same note about "tests' contents do not match their descriptions".

end
end

Expand Down
4 changes: 2 additions & 2 deletions test/controllers/demo_user_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
end

it 'should define render_authenticate_user_error' do
assert @controller.methods.include?(:render_authenticate_user_error)
assert @controller.methods.include?(:render_authenticate_error)
end

it 'should define render_authenticate_mang_error' do
assert @controller.methods.include?(:render_authenticate_mang_error)
assert @controller.methods.include?(:render_authenticate_error)
Copy link
Contributor

Choose a reason for hiding this comment

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

(And here, too.)

Perhaps there's more investigation to be made?

Copy link
Contributor

Choose a reason for hiding this comment

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

(Or, rather, renaming some tests?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh sorry 😱
I will modify bad tests.

end
end

Expand Down