shopify_app: Token exchange not triggered after app reinstall

I’m using token exchange auth and managed installs.

When I install the app for the first time - the new Shop record is created and ShopifyApp::Auth::TokenExchange is called.

When I re-install the app - the Shop record is already present in DB and ShopifyApp::Auth::TokenExchange not called automatically so shopify_token is not updated and post_authenticate_jobs are not called.

Expected behavior

ShopifyApp::Auth::TokenExchange should be called after app re-install.

Actual behavior

ShopifyApp::Auth::TokenExchange not called after re-install.

Demo

https://github.com/user-attachments/assets/8acbbaaf-a059-439a-87e7-79cc2e2599e5

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 2
  • Comments: 18 (9 by maintainers)

Most upvoted comments

Having the same issue, please help

Destroying Shop is not suitable for production apps, unfortunately. We need to keep data during grace period until fully erasing them. And even after erasing app data, we still need to keep Shop record without personal data to track possible app reinstalls in the future and offer support. We need a better default for handling uninstalls in shopify_app template.

Regarding suggested workaround with uninstalled column. In my apps I’m using uninstalled_at column to detect uninstalls too. However, this solely relies on webhooks and it won’t work in cases when the app re-installed right away after uninstall. This is not common among case among merchants, but this is specific case that Shopify App Store review team check apps for. So we have to handle it properly just to pass regular app review. And we can’t solely rely on webhooks - we need to perform exchange properly after each app reinstall automatically.

Hey Kirill,

thanks for reporting this issue, I’ve tagged the team working on this!

Nearly two months have passed on this issue, and while the robots have yet to inform us it is closed due to inactivity, it would be nice to know if there is any activity in this reasonably interesting case. Is there anything new to report?

@remy727 you have to disable it in ShopifyApp config to avoid duplication:

ShopifyApp.configure do |config|
  # Moved to Shop model because shopify_app doesn't trigger after_authenticate after reinstall
  # config.after_authenticate_job = {job: "PlatmartApp::AfterAuthenticateJob", inline: true}
end

hey @zzooeeyy! this issue is super important since it might cause an app to be delisted during the Shopify review. I’d be happy to help fix it in any way.

@kirillplatonov @zzooeeyy i have this workaround which might be good for reinstallation

  1. upon uninstall replace shop token with ‘na’ (also override model validations to enable this)
  2. override construct_session in shop model to return early if token is ‘na’
def self.def self.construct_session(shop)
    return unless shop
    return if shop.token == "na"

    ShopifyAPI::Auth::Session.new(
      shop: shop.shopify_domain,
      access_token: shop.shopify_token,
      scope: shop.access_scopes || ShopifyApp.configuration.scope
    )
  end 

this forces a token exchange from what i see so you would not need to override the controller.

Current workaround

To fix this issue in my app I end up adding controller callback that triggers token exchange on the first app loading from admin using pretty complex condition:

class AuthenticatedController < ApplicationController
  before_action :perform_token_exchange

  def perform_token_exchange
    return if params[:embedded] != "1" || params[:id_token].blank?
    return unless request.path == "/home"

    new_session = ShopifyApp::Auth::TokenExchange.perform(shopify_id_token)
    current_shopify_session.copy_attributes_from(new_session)
  end
end

And I now have to trigger AfterAuthenticateJob manually from Shop model to be sure it will fire only after install/re-install, when shopify_token is updated:

class Shop < ActiveRecord::Base
  after_save_commit :check_shopify_token_change

  def check_shopify_token_change
    return unless shopify_token_previously_changed?

    AfterAuthenticateJob.perform_now(shop_domain: shopify_domain)
  end
end

It works well but it’s a pretty hacky solution.

Proper fix

To properly fix the issue, we need to have some indication from ShopifyAdmin/AppBridge that the request is coming to the app after managed installation. Right now there’s no difference in regular app visit and app installation. The best fix would be providing a parameter (eg installed=1) to the first app visit after managed installation. This way we could trigger token exchange when we detect this parameter.