ShopOrigin and shat

Droppx
New Member
5 0 0

Hi guys,

I don't know how to get shopOrigin and shat without using cookies after shopify changed to using session tokens

server.use(
    createShopifyAuth({
      apiKey: SHOPIFY_API_KEY,
      secret: SHOPIFY_API_SECRET,
      scopes: [SCOPES],

      async afterAuth(ctx) {
        //Auth token and shop available in session
        //Redirect to shop upon auth
        const { shop, accessToken } = ctx.session;
        ctx.cookies.set("shopOrigin", shop, {
          httpOnly: false,
          secure: true,
          sameSite: "none",
        });
        ctx.cookies.set("shat", accessToken, {
          httpOnly: false,
          secure: true,
          sameSite: "none",
        });
        ctx.redirect("/");
      },
    })
  );


 router.get("/api/getwebhooks/:object", async (ctx) => {
    const url =
      "https://" +
      ctx.cookies.get("shopOrigin") +
      "/admin/api/2020-07/" +
      ctx.params.object +
      ".json";
    try {
      const getAllSubWebhooks = await fetch(url, {
        headers: {
          "X-Shopify-Access-Token": ctx.cookies.get("shat"),
          "Content-Type": "application/json",
        },
        method: "GET",
      })
        .then((response) => response.json())
        .then((json) => {
          return json;
        });
      ctx.res.statusCode = 200;
      ctx.body = {
        status: "success",
        data: getAllSubWebhooks,
      };
    } catch (err) {
      ctx.res.statusCode = 400;
      ctx.body = {
        status: "fail",
        error: err,
      };
    }
  });

From the code above you can see that I was able to use cookies to get shopOrigin and shat but after the change to session tokens, I do not know hot to get shopOrigin and shat in api/getwebhooks API

Below is the new Auth implementation

server.use(
    createShopifyAuth({
      async 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(`/?shop=${shop}&host=${host}`);
      },
    })
  );

 

0 Likes