Shopify / shopify_app

A Rails Engine for building Shopify Apps
MIT License
1.74k stars 683 forks source link

Handle invalid access tokens in token exchange #1822

Closed zzooeeyy closed 2 months ago

zzooeeyy commented 3 months ago

📹 Demo video

What this PR does

So developers handling their own errors can still have their access tokens refetched:

# my_controller.rb

def count_and_rescue
  with_token_refetch(current_shopify_session, shopify_id_token) do
    ShopifyAPI::Product.count
  end
rescue =>
  # performing error handling
end

Checklist

Before submitting the PR, please consider if any of the following are needed:

rachel-carvalho commented 3 months ago

We'll create an API wrapper and handle errors differently, so I'm moving this back to draft.

rachel-carvalho commented 2 months ago

We decided to keep retrying in the around_action, but will also introduce a module ShopifyApp::AdminAPI::WithTokenRefetch that offers a method that will automatically re-fetch an access token on Unauthorized errors and retry the block.

with_token_refetch(@session, @session_token) do
  # Admin API call here
end
rachel-carvalho commented 2 months ago

The tests that execute the new Session#copy_attributes_from method are now failing until we update the shopify_api version to include it.