Shopify / koa-shopify-auth

DEPRECATED Middleware to authenticate a Koa application with Shopify
MIT License
80 stars 63 forks source link

500 error on app install: Cannot complete OAuth process. Could not find an OAuth cookie for shop url #125

Closed david-wb closed 1 year ago

david-wb commented 3 years ago

Issue summary

Our non-embedded Shopify App has been installed without issues on a few shops, but one shop in particular is receiving the following 500 server error when trying to install.

Cannot complete OAuth process. Could not find an OAuth cookie for shop url

Expected behavior

The app should install without issues on any store.

Actual behavior

A 500 error is received when our client tries to install the app on their store.

The shopify context is being initialized like so:


Shopify.Context.initialize({
  API_KEY: process.env.SHOPIFY_API_KEY,
  API_SECRET_KEY: process.env.SHOPIFY_API_SECRET,
  SCOPES: process.env.SHOPIFY_API_SCOPES.split(","),
  HOST_NAME: process.env.SHOPIFY_APP_URL.replace(/https:\/\//, ""),
  API_VERSION: ShopifyApiVersion.July21,
  IS_EMBEDDED_APP: false,
  SESSION_STORAGE: new Shopify.Session.CustomSessionStorage(
    sessionStorage.storeCallback,
    sessionStorage.loadCallback,
    sessionStorage.deleteCallback
  ),
});

And the createShopifyAuth middleware is setup as follows

server.use(
  createShopifyAuth({
    accessMode: "offline",
    async afterAuth(ctx) {
      console.log("ctx.state.shopify:");
      console.log(JSON.stringify(ctx.state.shopify, null, 2));
      const { shop, scope, accessToken } = ctx.state.shopify;
      console.log("After auth");
      console.log("shop: ", shop);
      console.log("scope: ", scope);
      console.log("accessToken: ", Boolean(accessToken));

      const existingShop = await firestoreUtils.getShop(shop);
      if (existingShop) {
        console.log("A record for this shop already exists.");
      }

      if (shop && scope && accessToken) {
        console.log("Saving shop info.");
        await firestoreUtils.saveShop(shop, scope, accessToken);
      } else {
        console.log("Not saving shop info.");
      }

      const registration = await registerWebhook({
        address: `${process.env.SHOPIFY_APP_URL}/webhooks/app-uninstalled`,
        topic: "APP_UNINSTALLED",
        accessToken,
        shop,
        deliveryMethod: DeliveryMethod.Http,
        apiVersion: "2021-07",
      });

      if (registration.success) {
        console.info("Successfully registered APP_UNINSTALLED webhook.");
      } else {
        console.warn("Failed to register APP_UNINSTALLED webhook");
      }

      const subscriptionRegistration = await registerWebhook({
        address: `${process.env.SHOPIFY_APP_URL}/webhooks/app-subscription-updated`,
        topic: "APP_SUBSCRIPTIONS_UPDATE",
        accessToken,
        shop,
        deliveryMethod: DeliveryMethod.Http,
        apiVersion: "2021-07",
      });
      if (subscriptionRegistration.success) {
        console.info(
          "Successfully registered APP_SUBSCRIPTIONS_UPDATE webhook."
        );
      } else {
        console.warn("Failed to register APP_SUBSCRIPTIONS_UPDATE webhook");
      }

      ctx.redirect(
        getCWAShopifyInstallUrl(shop, accessToken, existingShop?.workspaceId)
      );
    },
  })
);

Checklist

github-actions[bot] commented 1 year ago

Note that this repo is no longer maintained and this issue will not be reviewed. Prefer the official JavaScript API library. If you still want to use Koa, see simple-koa-shopify-auth for a potential community solution.