Join us for an upcoming Shopify Partner webinar on February 27, 2024. Discover the latest Checkout Extensibility features, and deep dive on improvements to Shopify Functions and Web Pixels. Register now for either the 10am EST or 2pm EST sessions.
Solved

koa-shopify-auth too many redirects

Mykhailo
Shopify Partner
9 1 5

Hello community!

I am trying to deploy Node Shopify Public App on different domains for Frontend and Backend.

Each time I call https://<MY_BACKEND_PATH>/auth?shop=<MY_SHOP> I get HTTP 302 with Location same as original request https://<MY_BACKEND_PATH>/auth?shop=<MY_SHOP>

I have NextJS run as static site on another domain. I have provided all correct Env variables to my Backend. I am using latest Shopify CLI. I have updated my Partner Dashboard correctly.

All is working fine with shopify node server i.e. with ngrok but fails in real life.

What have I missed?

Accepted Solution (1)
Mykhailo
Shopify Partner
9 1 5

This is an accepted solution.

ok, seems having prefix in afterAuth is bad idea since it will not work for case when Koa deployed not on root /. Creating domain name and assigning it to my host's / solved the problem

View solution in original post

Replies 2 (2)

Mykhailo
Shopify Partner
9 1 5

ok, found issue... that's because I need to use prefix for Koa auth lib. Since I need to prepend each request with /dev/index I enforced to add 

prefix: "/dev/index/
 
to my createShopifyAuth method. Any idea how to overcome this?
 

 

server.use(
  createShopifyAuth({
    prefix: "/dev/index",// I need this to have /dev/index/auth/inline redirect but not /auth/inline since it is not served by my Backend
    async afterAuth(ctx) {
      console.log("afterAuth", ctx);
      // Access token and shop available in ctx.state.shopify
      const { shop, accessToken, scope } = ctx.state.shopify;
      const host = ctx.query.host;
      ACTIVE_SHOPIFY_SHOPS[shop] = scope;

      const response = await Shopify.Webhooks.Registry.register({
        shop,
        accessToken,
        path: "/webhooks",
        topic: "APP_UNINSTALLED",
        webhookHandler: async (topic, shop, body) =>
          delete ACTIVE_SHOPIFY_SHOPS[shop],
      });

      if (!response.success) {
        console.log(
          `Failed to register APP_UNINSTALLED webhook: ${response.result}`
        );
      }

      // Redirect to app with shop parameter upon auth
      ctx.redirect(`${process.env.BACKEND_URL}/?shop=${shop}&host=${host}`);
    },
  })
);

 

Mykhailo
Shopify Partner
9 1 5

This is an accepted solution.

ok, seems having prefix in afterAuth is bad idea since it will not work for case when Koa deployed not on root /. Creating domain name and assigning it to my host's / solved the problem