Skip to content

Commit

Permalink
Fix, Avoid checking resources multi times
Browse files Browse the repository at this point in the history
  • Loading branch information
leanhthang committed Jan 28, 2021
1 parent c922580 commit 0968c7a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 27 deletions.
14 changes: 12 additions & 2 deletions app/controllers/devise_token_auth/concerns/set_user_by_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def set_request_start

# user auth
def set_user_by_token(mapping = nil)
# Avoid checking resources multi times
return @resource if resource_is_checked?(mapping)

# determine target authentication class
rc = resource_class(mapping)

Expand Down Expand Up @@ -76,12 +79,13 @@ def set_user_by_token(mapping = nil)
else
sign_in(scope, user, store: false, event: :fetch, bypass: DeviseTokenAuth.bypass_sign_in)
end
return @resource = user
@resource = user
else
# zero all values previously set values
@token.client = nil
return @resource = nil
@resource = nil
end
@resource
end

def update_auth_header
Expand Down Expand Up @@ -159,4 +163,10 @@ def auth_header_from_batch_request
end
auth_header
end

def resource_is_checked?(mapping)
return true if @resource || eval("@has_checked_#{mapping}") == true
eval("@has_checked_#{mapping} = true")
return false
end
end
8 changes: 0 additions & 8 deletions test/controllers/demo_group_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest
assert @controller.user_signed_in?
end

it 'should not define current_mang' do
refute_equal @resource, @controller.current_mang
end

it 'should define current_member' do
assert_equal @resource, @controller.current_member
end
Expand Down Expand Up @@ -109,10 +105,6 @@ class DemoGroupControllerTest < ActionDispatch::IntegrationTest
assert @controller.mang_signed_in?
end

it 'should not define current_mang' do
refute_equal @mang, @controller.current_user
end

it 'should define current_member' do
assert_equal @mang, @controller.current_member
end
Expand Down
4 changes: 0 additions & 4 deletions test/controllers/demo_mang_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ class DemoMangControllerTest < ActionDispatch::IntegrationTest
assert @controller.mang_signed_in?
end

it 'should not define current_user' do
refute_equal @resource, @controller.current_user
end

it 'should define render_authenticate_error' do
assert @controller.methods.include?(:render_authenticate_error)
end
Expand Down
13 changes: 0 additions & 13 deletions test/controllers/demo_user_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
assert @controller.user_signed_in?
end

it 'should not define current_mang' do
refute_equal @resource, @controller.current_mang
end

it 'should define render_authenticate_error' do
assert @controller.methods.include?(:render_authenticate_error)
end
Expand Down Expand Up @@ -539,11 +535,6 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
it 'should define user_signed_in?' do
assert @controller.user_signed_in?
end

it 'should not define current_mang' do
refute_equal @resource, @controller.current_mang
end

end

it 'should return success status' do
Expand Down Expand Up @@ -590,10 +581,6 @@ class DemoUserControllerTest < ActionDispatch::IntegrationTest
it 'should define user_signed_in?' do
assert @controller.user_signed_in?
end

it 'should not define current_mang' do
refute_equal @resource, @controller.current_mang
end
end

it 'should return success status' do
Expand Down

0 comments on commit 0968c7a

Please sign in to comment.