Skip to content

Commit 9fe98a9

Browse files
authored
Merge pull request #869 from abeyuya/feature/customable-authorized_users_only_error-response
Feature/customable authorized users only error response
2 parents 9194a51 + 1b141ce commit 9fe98a9

File tree

4 files changed

+47
-22
lines changed

4 files changed

+47
-22
lines changed

lib/devise_token_auth/controllers/helpers.rb

+30-20
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ module ClassMethods
1515
# devise_group :blogger, contains: [:user, :admin]
1616
#
1717
# Generated methods:
18-
# authenticate_blogger! # Redirects unless user or admin are signed in
19-
# blogger_signed_in? # Checks whether there is either a user or an admin signed in
20-
# current_blogger # Currently signed in user or admin
21-
# current_bloggers # Currently signed in user and admin
18+
# authenticate_blogger! # Redirects unless user or admin are signed in
19+
# blogger_signed_in? # Checks whether there is either a user or an admin signed in
20+
# current_blogger # Currently signed in user or admin
21+
# current_bloggers # Currently signed in user and admin
22+
# render_authenticate_error # Render error unless user or admin are signed in
2223
#
2324
# Use:
2425
# before_action :authenticate_blogger! # Redirects unless either a user or an admin are authenticated
@@ -38,9 +39,7 @@ def authenticate_#{group_name}!(favourite=nil, opts={})
3839
end
3940
4041
unless current_#{group_name}
41-
return render json: {
42-
errors: [I18n.t('devise.failure.unauthenticated')]
43-
}, status: 401
42+
render_authenticate_error
4443
end
4544
end
4645
end
@@ -67,8 +66,14 @@ def current_#{group_name.to_s.pluralize}
6766
end.compact
6867
end
6968
69+
def render_authenticate_error
70+
return render json: {
71+
errors: [I18n.t('devise.failure.unauthenticated')]
72+
}, status: 401
73+
end
74+
7075
if respond_to?(:helper_method)
71-
helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?"
76+
helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?", "render_authenticate_error"
7277
end
7378
METHODS
7479
end
@@ -90,14 +95,15 @@ def log_process_action(payload)
9095
# Admin
9196
#
9297
# Generated methods:
93-
# authenticate_user! # Signs user in or 401
94-
# authenticate_admin! # Signs admin in or 401
95-
# user_signed_in? # Checks whether there is a user signed in or not
96-
# admin_signed_in? # Checks whether there is an admin signed in or not
97-
# current_user # Current signed in user
98-
# current_admin # Current signed in admin
99-
# user_session # Session data available only to the user scope
100-
# admin_session # Session data available only to the admin scope
98+
# authenticate_user! # Signs user in or 401
99+
# authenticate_admin! # Signs admin in or 401
100+
# user_signed_in? # Checks whether there is a user signed in or not
101+
# admin_signed_in? # Checks whether there is an admin signed in or not
102+
# current_user # Current signed in user
103+
# current_admin # Current signed in admin
104+
# user_session # Session data available only to the user scope
105+
# admin_session # Session data available only to the admin scope
106+
# render_authenticate_error # Render error unless user or admin is signed in
101107
#
102108
# Use:
103109
# before_action :authenticate_user! # Tell devise to use :user map
@@ -109,9 +115,7 @@ def self.define_helpers(mapping) #:nodoc:
109115
class_eval <<-METHODS, __FILE__, __LINE__ + 1
110116
def authenticate_#{mapping}!
111117
unless current_#{mapping}
112-
return render json: {
113-
errors: [I18n.t('devise.failure.unauthenticated')]
114-
}, status: 401
118+
render_authenticate_error
115119
end
116120
end
117121
@@ -126,11 +130,17 @@ def current_#{mapping}
126130
def #{mapping}_session
127131
current_#{mapping} && warden.session(:#{mapping})
128132
end
133+
134+
def render_authenticate_error
135+
return render json: {
136+
errors: [I18n.t('devise.failure.unauthenticated')]
137+
}, status: 401
138+
end
129139
METHODS
130140

131141
ActiveSupport.on_load(:action_controller) do
132142
if respond_to?(:helper_method)
133-
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
143+
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session", "render_authenticate_error"
134144
end
135145
end
136146
end

test/controllers/demo_group_controller_test.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest
7474
it 'should define member_signed_in?' do
7575
assert @controller.current_members.include? @resource
7676
end
77+
78+
it 'should define render_authenticate_error' do
79+
assert @controller.methods.include?(:render_authenticate_error)
80+
end
7781
end
7882
end
7983

@@ -118,6 +122,10 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest
118122
it 'should define member_signed_in?' do
119123
assert @controller.current_members.include? @mang
120124
end
125+
126+
it 'should define render_authenticate_error' do
127+
assert @controller.methods.include?(:render_authenticate_error)
128+
end
121129
end
122130
end
123131

@@ -132,7 +140,7 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest
132140

133141
it 'should return error: unauthorized status' do
134142
assert_equal 401, response.status
135-
end
143+
end
136144
end
137145
end
138146
end

test/controllers/demo_mang_controller_test.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest
4646
it 'should not define current_user' do
4747
refute_equal @resource, @controller.current_user
4848
end
49+
50+
it 'should define render_authenticate_error' do
51+
assert @controller.methods.include?(:render_authenticate_error)
52+
end
4953
end
5054

5155
it 'should return success status' do
@@ -260,4 +264,3 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest
260264
end
261265
end
262266
end
263-

test/controllers/demo_user_controller_test.rb

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
4747
it 'should not define current_mang' do
4848
refute_equal @resource, @controller.current_mang
4949
end
50+
51+
it 'should define render_authenticate_error' do
52+
assert @controller.methods.include?(:render_authenticate_error)
53+
end
5054
end
5155

5256
it 'should return success status' do

0 commit comments

Comments
 (0)