Help needed react and node.js app

Solved
Highlighted
Excursionist
58 2 1

Hi so I have never developed a shopify app before and I am following this tutorial https://shopify.dev/tutorials/build-a-shopify-app-with-node-and-react so I have created a server.js file and have this code:

 

app.prepare().then(() => {
  const server = new Koa();
  server.use(session({ secure: true, sameSite: 'none' }, server));
  server.keys = [SHOPIFY_API_SECRET_KEY];

  server.use(
    createShopifyAuth({
      apiKey: SHOPIFY_API_KEY,
      secret: SHOPIFY_API_SECRET_KEY,
      scopes: ['read_themes', 'write_themes'],
      afterAuth(ctx) {
        const { shop, accessToken } = ctx.session;
        ctx.cookies.set('shopOrigin', shop, {
          httpOnly: false,
          secure: true,
          sameSite: 'none'
        });
        ctx.cookies.set('accessToken', accessToken);
                  console.log(`${shop}`);
        console.log(`${accessToken}`);
                  
        
        ctx.redirect('/');
      },
        
    }),
    
     
  );

How would I be able to access shop and accessToken from another file like customer.js?

 

So for example if inside the customer.js file I have a shopify polaris page setup and there was a button that was called Add Customer, I would have fetch link that would grab the /admin/api/2020-01/customers.json but I will need to be able to access the shop and accessToken from server.js file in order to be able to run the fetch, however, at the moment I am unable to access the server.js file.

0 Likes
Highlighted
Shopify Staff
Shopify Staff
1041 140 165

This is an accepted solution.

Hey @benjk,

 

From that example it looks like that information is available from the session and cookies.

 

Server side: 

const { shop, accessToken } = ctx.session

 

Client side:

Cookies.get("shopOrigin")
Cookies.get("accessToken")
0 Likes
Highlighted
Excursionist
58 2 1

Okay I got it I thought you had to use ${shop} because that wasn't working for me

0 Likes
Highlighted
Tourist
11 0 1

Heey, can you help me?
I have a Koa backend and a React frontend. I'm using cookies to send shopOrigin, but on some browsers (Safari) and incognito modes, this doesn't work... How can I send shopOrigin from Koa to AppBrdige in frontend in another way?

I've been searching al day long... no one answers me here

0 Likes