I've recently had the case that a user un-installed my Shopify app and instantly re-installed it. This caused a problem because I store all my users in a DB table.
Login/Installing works as follows:
- The user tells me his shop URL
- I forward the user to example.myshopify.com/admin/oauth/authorize where access is granted to my app
- I check if that shop URL is already stored in my local
user DB
- If not: I request a permanent access token and forward the user to the plan selection page
- If yes: I get the stored permanent access token from the user DB and log the user into my app
Uninstalling:
- The user uninstalls my app in his Shopify backend
- Shopify sends a webhook to my app
- I remove that user's data from the user DB
The problem is that the webhooks are sometimes delayed. If an user uninstalls and instantly re-installs, my app will think the install is a login attempt, and will use the now invalid access token stored in the user DB.
I figured I could just check if the redirection from the authorization page contains a temporary access token, and if yes, it would be a new installation, but it seems the access token is returned even if the app has already been installed.
So my question is: How can I handle instant re-installation gracefully? Surely there's something that I'm overlooking, there can't be such a huge "logic bug" in the Shopify API?