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)
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
uninstalledcolumn. In my apps I’m usinguninstalled_atcolumn 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:
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
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:
And I now have to trigger
AfterAuthenticateJobmanually fromShopmodel to be sure it will fire only after install/re-install, whenshopify_tokenis updated: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.