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

Don't delete session on 401 error #1844

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Unreleased
----------
* Patch - Don't delete session on 401 errors during retry in `with_token_refetch` [#1844](https://github.com/Shopify/shopify_app/pull/1844)

22.2.0 (May 2,2024)
----------
Expand Down
3 changes: 1 addition & 2 deletions lib/shopify_app/admin_api/with_token_refetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def with_token_refetch(session, shopify_id_token)
ShopifyApp::Logger.debug("Encountered error: #{error.code} - #{error.response.inspect}, re-raising")
elsif retrying
ShopifyApp::Logger.debug("Shopify API returned a 401 Unauthorized error that was not corrected " \
"with token exchange, deleting current session and re-raising")
ShopifyApp::SessionRepository.delete_session(session.id)
"with token exchange, re-raising error")
else
retrying = true
ShopifyApp::Logger.debug("Shopify API returned a 401 Unauthorized error, exchanging token and " \
Expand Down
5 changes: 2 additions & 3 deletions test/shopify_app/admin_api/with_token_refetch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def setup
assert_equal @new_session.expires, @session.expires
end

test "#with_token_refetch deletes existing token and re-raises when 401 persists" do
test "#with_token_refetch re-raises when 401 persists" do
response = ShopifyAPI::Clients::HttpResponse.new(code: 401, body: "401 message", headers: {})
api_error = ShopifyAPI::Errors::HttpResponseError.new(response: response)

Expand All @@ -67,8 +67,7 @@ def setup
"and retrying with new session")

ShopifyApp::Logger.expects(:debug).with("Shopify API returned a 401 Unauthorized error that was not corrected " \
"with token exchange, deleting current session and re-raising")
ShopifyApp::SessionRepository.expects(:delete_session).with("session-id")
"with token exchange, re-raising error")

reraised_error = assert_raises ShopifyAPI::Errors::HttpResponseError do
with_token_refetch(@session, @id_token) do
Expand Down
Loading