Skip to content

Commit 8900dda

Browse files
add hack to fix issue with new admin.shopify.com domains
1 parent 2cc89d9 commit 8900dda

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-koa-shopify-auth",
3-
"version": "2.1.6",
3+
"version": "2.1.7",
44
"description": "A better, simplified version of the (no longer supported) @Shopify/koa-shopify-auth middleware library. It removes the use of cookies for sessions (which greatly smooths the auth process), replaces a deprecated API call, and supports v2 of the official @shopify/shopify-api package.",
55
"author": "TheSecurityDev",
66
"license": "MIT",

src/top-level-oauth-redirect.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,54 @@ export async function startTopLevelOauthRedirect(ctx: Context, apiKey: string, p
4747
}
4848

4949
async function getTopLevelRedirectScript(host: string, redirectTo: string, apiKey: string) {
50+
let shopName = "";
51+
try {
52+
const decodedHost = Buffer.from(host, "base64").toString("utf8");
53+
const shopFromOldHost = decodedHost.match(/([\w-]*).myshopify.com\/admin/);
54+
const shopFromNewHost = decodedHost.match(/admin.shopify.com\/store\/([\w-]*)/);
55+
shopName = shopFromNewHost ? shopFromNewHost[1] : shopFromOldHost ? shopFromOldHost[1] : "";
56+
} catch (error) {
57+
console.error("Error decoding host", error);
58+
}
5059
// We used to load the script from unpkg.com, but that sometimes was too slow, so we are now loading the script file directly and injecting the code.
5160
const appBridgeScript = await readFile(APP_BRIDGE_FILE_PATH);
5261
return `
5362
<!-- Shopify App Bridge -->
5463
<script type="text/javascript">${appBridgeScript}</script>
5564
<script type="text/javascript">
5665
document.addEventListener('DOMContentLoaded', function() {
66+
const apiKey = '${apiKey}';
67+
const redirectUrl = '${redirectTo}';
5768
if (window.top === window.self) {
5869
// If the current window is the 'parent', change the URL by setting location.href
59-
window.location.href = "${redirectTo}";
70+
window.location.href = redirectUrl;
6071
} else {
6172
// If the current window is the 'child', change the parent's URL with postMessage
6273
var AppBridge = window['app-bridge'];
6374
var createApp = AppBridge.default;
6475
var Redirect = AppBridge.actions.Redirect;
65-
var app = createApp({
66-
apiKey: "${apiKey}",
67-
host: "${encodeURI(host)}",
68-
});
69-
var redirect = Redirect.create(app);
70-
redirect.dispatch(Redirect.Action.REMOTE, "${redirectTo}");
76+
try {
77+
var app = createApp({
78+
apiKey,
79+
host: "${encodeURI(host)}",
80+
});
81+
var redirect = Redirect.create(app);
82+
redirect.dispatch(Redirect.Action.REMOTE, redirectUrl);
83+
} catch (e) {
84+
console.error(e);
85+
}
86+
try {
87+
// For some reason, we get the old host parameter sometimes when using the new admin.shopify.com domain, and this causes issues with the redirect.
88+
// So we will create a second redirect using the new host, just in case.
89+
var app = createApp({
90+
apiKey,
91+
host: "${encodeURI(btoa(`admin.shopify.com/store/${shopName}`))}",
92+
});
93+
var redirect = Redirect.create(app);
94+
redirect.dispatch(Redirect.Action.REMOTE, redirectUrl);
95+
} catch (e) {
96+
console.error(e);
97+
}
7198
}
7299
});
73100
</script>

0 commit comments

Comments
 (0)