FROM CACHE - en_header

Koa server-- How to pass data in the server between middlewares so I can pass it to the front end

New Member
1 0 0

In my server, I have the following set up to grab the current logged-in user data:

  const server = new Koa();
  const router = new Router();
  server.keys = [Shopify.Context.API_SECRET_KEY];
      async afterAuth(ctx) {
        // Access token and shop available in ctx.state.shopify
        const { shop, accessToken, scope } = ctx.state.shopify;

  const client = new Shopify.Clients.Rest(shop, accessToken);
        const data = await client.get({
          path: 'users/current',

I am correctly getting the data but I would like to pass it to the front end. I've tried storing the data in a global variable, I've tried storing the data in app.context and I've tried storing the data in ctx.state.

My idea is that I'd be able to grab the data and place it in the following so that I can make a fetch request to this /user endpoint:

router.get('/user', (ctx,next) => {
  console.log('check for ctx.state: ', ctx.state);

At the end of the day, I need access to the shop and accessToken from the Auth function which is why I'd need to pass the data instead of making the data request in the router.get function. I haven't worked with Koa before so any help would be greatly appreciated. Thank you!

Replies 0 (0)