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

50 changes: 30 additions & 20 deletions lib/devise_token_auth/controllers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ module ClassMethods
# devise_group :blogger, contains: [:user, :admin]
#
# Generated methods:
# authenticate_blogger! # Redirects unless user or admin are signed in
# 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
# authenticate_blogger! # Redirects unless user or admin are signed in
# 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
#
# Use:
# before_action :authenticate_blogger! # Redirects unless either a user or an admin are authenticated
Expand All @@ -38,9 +39,7 @@ def authenticate_#{group_name}!(favourite=nil, opts={})
end

unless current_#{group_name}
return render json: {
errors: ["Authorized users only."]
}, status: 401
render_authenticate_#{group_name}_error
end
end
end
Expand All @@ -67,8 +66,14 @@ def current_#{group_name.to_s.pluralize}
end.compact
end

def render_authenticate_#{group_name}_error
return render json: {
errors: ["Authorized #{group_name} only."]
}, status: 401
end

if respond_to?(:helper_method)
helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?"
helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?", "render_authenticate_#{group_name}_error"
end
METHODS
end
Expand All @@ -90,14 +95,15 @@ def log_process_action(payload)
# Admin
#
# Generated methods:
# authenticate_user! # Signs user in or 401
# authenticate_admin! # Signs admin in or 401
# user_signed_in? # Checks whether there is a user signed in or not
# admin_signed_in? # Checks whether there is an admin signed in or not
# current_user # Current signed in user
# 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
# authenticate_user! # Signs user in or 401
# authenticate_admin! # Signs admin in or 401
# user_signed_in? # Checks whether there is a user signed in or not
# admin_signed_in? # Checks whether there is an admin signed in or not
# current_user # Current signed in user
# 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_#{mapping}_error # Render error if
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps this docs line needs to be 2 lines (one for each generated mapping).

And the comment at the end of the line seems unfinished.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean: have two lines, with ending comments like these:

  • # Render error unless user is signed in
  • # Render error unless admin is signed in

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@olleolleolle Thank you for your review.
You are right. I will modify the comment soom.

Copy link
Contributor

Choose a reason for hiding this comment

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

That was fast.

#
# Use:
# before_action :authenticate_user! # Tell devise to use :user map
Expand All @@ -109,9 +115,7 @@ def self.define_helpers(mapping) #:nodoc:
class_eval <<-METHODS, __FILE__, __LINE__ + 1
def authenticate_#{mapping}!
unless current_#{mapping}
return render json: {
errors: ["Authorized users only."]
}, status: 401
render_authenticate_#{mapping}_error
end
end

Expand All @@ -126,11 +130,17 @@ def current_#{mapping}
def #{mapping}_session
current_#{mapping} && warden.session(:#{mapping})
end

def render_authenticate_#{mapping}_error
return render json: {
errors: ["Authorized #{mapping} only."]
}, status: 401
end
METHODS

ActiveSupport.on_load(:action_controller) do
if respond_to?(:helper_method)
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session", "render_authenticate_#{mapping}_error"
end
end
end
Expand Down
14 changes: 13 additions & 1 deletion test/controllers/demo_group_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest
it 'should define member_signed_in?' do
assert @controller.current_members.include? @resource
end

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

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

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

Expand Down Expand Up @@ -132,7 +144,7 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest

it 'should return error: unauthorized status' do
assert_equal 401, response.status
end
end
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion test/controllers/demo_mang_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest
it 'should not define current_user' do
refute_equal @resource, @controller.current_user
end

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

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

it 'should return success status' do
Expand Down Expand Up @@ -260,4 +268,3 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest
end
end
end

8 changes: 8 additions & 0 deletions test/controllers/demo_user_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
it 'should not define current_mang' do
refute_equal @resource, @controller.current_mang
end

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

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

it 'should return success status' do
Expand Down