Migrating Shopify App to use Session Tokens Difficulties

Been working the past few days on migrating our Shopify app from using Cookies for authorization to using the new session tokens. I’ve followed a lot of the code examples but they are very lacking with actual examples of how this used.

Our main issue is that verifyRequest always redirects to authenticate no matter how many times we load the app as if the session isn’t getting saved. We are also unable to ever call authenticatedFetch or getSessionToken, the promise never resolves.

The token below is never logged and we’ve verified that no calls are ever made.

import { Page } from "@shopify/polaris";
import React, { useEffect } from "react";
import { getSessionToken } from "@shopify/app-bridge-utils";
import { useAppBridge } from "@shopify/app-bridge-react";
import { useRouter } from "next/router";

const Index = () => {
  const router = useRouter();
  const app = useAppBridge();
  useEffect(() => {
    console.log("here");
    console.log(app);
    getSessionToken(app)
      .then((token) => {
        console.log(token);
        router.push("/home/check").catch((e) => console.error(e));
      })
      .catch((e) => console.error(e));
  });

  const renderBody = () => ;
  return renderBody();
};

export default Index;

Our koa-router:

router.get("/", async (ctx) => {
  await handleRequest(ctx);
});

Another question we have is how navigation between tabs is supposed to work if each of these pages have to send the header with the session token? We have multiple pages that serve different purposes within the app.

Thanks in advance for any help!

Ok, I fixed this issue, it was because we were encoding the host with https:// in front for the config.
However, we now have a new issue where verifyRequest is redirecting to auth on an endpoint even though the session exists. I’ve verified that the session exists by removing the verifyRequest and printing the session:

const session = await Shopify.Utils.loadCurrentSession(ctx.req, ctx.res);
console.log(session);

which prints out what looks like a valid session