React App - Auth Flow

Highlighted
New Member
2 0 1

Hi all,

I am building an app and I have a question regarding the authentication flow. Following the official React App tutorial, I have created a method getSubscriptionUrl that will redirect clients to the screen to create a subscription. That the tutorials does not mention is that this flow will be executed every time the merchant launches the app and he will have to see the subscription screen everytime. What is the usualy approach to handling this? Checking if there is an existing subscription and skipping the subscription step or there is something better that is built-in?

Here is my server.js code and getSubscriptionUrl is the last method that is executed.

server.use(
    createShopifyAuth({
      apiKey: SHOPIFY_API_KEY,
      secret: SHOPIFY_API_SECRET_KEY,
      accessMode: "offline",
      scopes: ['read_products', 'write_products'],
      async afterAuth(ctx) {
        const { shop, accessToken } = ctx.session;

        ctx.cookies.set("shopOrigin", shop, { httpOnly: false });
        const connect = await fetch(SHOPIFY_BACKEND_HOST + "/api/storeconnection", {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            url: shop,
            accessToken: accessToken
          })
        });

        await getSubscriptionUrl(ctx, accessToken, shop, SHOPIFY_APP_HOST);
      }
    })
  );

How should I avoid the redirect to the subscription page for subsequent visits to the app?

0 Likes