Skip to content

Commit 189b42f

Browse files
made some changes to support Shopify Node API v5
1 parent cd993bd commit 189b42f

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
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": "1.0.5",
3+
"version": "1.0.6",
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/create-shopify-auth.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default function createShopifyAuth(options: OAuthBeginConfig) {
4848

4949
if (path === inlineOAuthPath || (path === oAuthStartPath && shouldPerformInlineOAuth(ctx))) {
5050
// Auth started
51-
if (!Shopify.Utils.validateShop(shop)) {
51+
if (!validateShop(shop)) {
5252
// Invalid shop
5353
ctx.response.status = 400;
5454
ctx.response.body = shop ? "Invalid shop parameter" : "Missing shop parameter";
@@ -107,3 +107,8 @@ export default function createShopifyAuth(options: OAuthBeginConfig) {
107107
await next();
108108
};
109109
}
110+
111+
export function validateShop(shop: string): boolean {
112+
const shopUrlRegex = /^[a-zA-Z0-9][a-zA-Z0-9-]*\.myshopify\.(com|io)[/]*$/;
113+
return shopUrlRegex.test(shop);
114+
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import createShopifyAuth from "./create-shopify-auth";
1+
import createShopifyAuth, { validateShop } from "./create-shopify-auth";
22
import verifyRequest from "./verify-request";
33

4-
export { createShopifyAuth, verifyRequest };
4+
export { createShopifyAuth, validateShop, verifyRequest };

src/verify-request.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ export default function verifyRequest(options?: VerifyRequestOptions) {
6363
await next();
6464
return;
6565
} catch (err) {
66-
if (err instanceof HttpResponseError && err.code == 401) {
66+
if (
67+
err instanceof HttpResponseError &&
68+
(err.code === 401 || (err as any).response?.code === 401) // Shopify API v3 uses 'response.code' instead of 'code'
69+
) {
6770
// Session not valid, we will re-authorize
6871
} else {
6972
throw err;

0 commit comments

Comments
 (0)