FWIW: My backend is Java, so I’m not using the Ruby or Node quick start libraries there. This is a little long, sorry!
I have my app working as standalone: OAuth install and authorization, standalone app page working, API calls to Shopify working, etc…
However, now I’m trying to get it to work as an embedded app instead, and running into issues. If the app was installed to my test store as standalone, and then I switch over to embedded mode, it works great. However if the app has not been installed, then things are failing to work the way I’d expect.
I’m new to this (Shopify app development, and React) so I may be making an obvious mistake somewhere here.
In standalone mode my main application page is a “protected page” via OAuth, so it automatically triggers the OAuth flow if it’s not already authorized.
In embedded mode, as I understand it, the main page has to be unprotected, load in AppBridge, check to see if the app has been installed for this store or not, if yes, then display the embedded admin page content, if no, then redirect to the OAuth install flow.
if (data.AuthCheckResponse.authenticated) {
console.log("authCheck success");
} else {
console.log("authCheck failed");
const apiKey = config.apiKey;
const redirectUri = "https://myngrokhostnamehere.ngrok.io/oauth2/authorization/shopify";
const permissionUrl = `https://${host}/admin/oauth/authorize?client_id=${apiKey}&scope=read_products,read_content&redirect_uri=${redirectUri}`;
console.log("Redirecting to: " + permissionUrl);
// window.location.href = data.AuthCheckResponse.authRedirectURL;
// If the current window is the 'parent', change the URL by setting location.href
if (window.top == window.self) {
window.location.assign(permissionUrl);
// If the current window is the 'child', change the parent's URL with Shopify App Bridge's Redirect action
} else {
Redirect.create(app).dispatch(Redirect.Action.REMOTE, permissionUrl);
}
}
What is happening is when I try to install the app, I get taken to my page (outside of the admin). At this point I would expect the code above to run, and perform a redirect if needed. However, what actually happens is I see this error in the browser’s console:
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('
Then the browser is redirected (by AppBridge?) to my test store's admin, with a URL like this: [https://my-test-store.myshopify.com/admin/apps/$longbase64value/dash-embedded?hmac=fabbe9da79caad69503c4b46527bd5124108270795c8df98cdffdde787f0506c&host=$myencodedhost&shop=my-test-store.myshopify.com×tamp=1647727209](https://my-test-store.myshopify.com/admin/apps/$longbase64value/dash-embedded?hmac=fabbe9da79caad69503c4b46527bd5124108270795c8df98cdffdde787f0506c&host=$myencodedhost&shop=my-test-store.myshopify.com×tamp=1647727209)
Which displays an embedded error page saying "There's no page at this address" - screenshot: [https://share.getcloudapp.com/nOuRNv0A](https://share.getcloudapp.com/nOuRNv0A)
After the redirect to the Shopify admin happens, there are no requests to my backend made. I don't know where the redirect to the admin is coming from, nor do I know why it isn't trying to load the embedded page from my backend.
Any ideas? I may well be doing something very wrong here, as the documentation around embedded app behavior assumes you're using their SDKs and a great deal of "magic" happens there.
Thanks!