Skip to content

Commit bd7242f

Browse files
committed
Fix, Avoid checking resources multi times
1 parent c922580 commit bd7242f

File tree

4 files changed

+12
-27
lines changed

4 files changed

+12
-27
lines changed

app/controllers/devise_token_auth/concerns/set_user_by_token.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def set_request_start
2424

2525
# user auth
2626
def set_user_by_token(mapping = nil)
27+
# Avoid checking resources multi times
28+
return @resource if set_user_by_token?(mapping)
29+
2730
# determine target authentication class
2831
rc = resource_class(mapping)
2932

@@ -76,12 +79,13 @@ def set_user_by_token(mapping = nil)
7679
else
7780
sign_in(scope, user, store: false, event: :fetch, bypass: DeviseTokenAuth.bypass_sign_in)
7881
end
79-
return @resource = user
82+
@resource = user
8083
else
8184
# zero all values previously set values
8285
@token.client = nil
83-
return @resource = nil
86+
@resource = nil
8487
end
88+
@resource
8589
end
8690

8791
def update_auth_header
@@ -159,4 +163,10 @@ def auth_header_from_batch_request
159163
end
160164
auth_header
161165
end
166+
167+
def resource_is_checked?(mapping)
168+
return true if @resource || eval("@has_checked_#{mapping}") == true
169+
eval("@has_checked_#{mapping} = true")
170+
return false
171+
end
162172
end

test/controllers/demo_group_controller_test.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest
5959
assert @controller.user_signed_in?
6060
end
6161

62-
it 'should not define current_mang' do
63-
refute_equal @resource, @controller.current_mang
64-
end
65-
6662
it 'should define current_member' do
6763
assert_equal @resource, @controller.current_member
6864
end
@@ -109,10 +105,6 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest
109105
assert @controller.mang_signed_in?
110106
end
111107

112-
it 'should not define current_mang' do
113-
refute_equal @mang, @controller.current_user
114-
end
115-
116108
it 'should define current_member' do
117109
assert_equal @mang, @controller.current_member
118110
end

test/controllers/demo_mang_controller_test.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest
4545
assert @controller.mang_signed_in?
4646
end
4747

48-
it 'should not define current_user' do
49-
refute_equal @resource, @controller.current_user
50-
end
51-
5248
it 'should define render_authenticate_error' do
5349
assert @controller.methods.include?(:render_authenticate_error)
5450
end

test/controllers/demo_user_controller_test.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
4646
assert @controller.user_signed_in?
4747
end
4848

49-
it 'should not define current_mang' do
50-
refute_equal @resource, @controller.current_mang
51-
end
52-
5349
it 'should define render_authenticate_error' do
5450
assert @controller.methods.include?(:render_authenticate_error)
5551
end
@@ -539,11 +535,6 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
539535
it 'should define user_signed_in?' do
540536
assert @controller.user_signed_in?
541537
end
542-
543-
it 'should not define current_mang' do
544-
refute_equal @resource, @controller.current_mang
545-
end
546-
547538
end
548539

549540
it 'should return success status' do
@@ -590,10 +581,6 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
590581
it 'should define user_signed_in?' do
591582
assert @controller.user_signed_in?
592583
end
593-
594-
it 'should not define current_mang' do
595-
refute_equal @resource, @controller.current_mang
596-
end
597584
end
598585

599586
it 'should return success status' do

0 commit comments

Comments
 (0)