shopOrigin must be provided- Error, Step 3 Build shopify add with Node and React Tutorial

Solved
ElizMartinez
New Member
1 0 0

Hello.
Thanks for the tutorial it worked as expected until step 3 add Shopify App Bridge. I have copied the code as instructed to test my configuration. When viewing the test app on the  "Unhandled Runtime Error AppBridgeError: APP::ERROR::INVALID_CONFIG: shopOrigin must be provided"Capture.JPG

this is the server js

Capture2.JPG

 

I have been reading and studying the text for the last two days. I have not added any custom code. Please provide some guidance.

Thanks

 

 

0 Likes
MichiBLN
Tourist
9 1 5

This is an accepted solution.

Hi, 

I had the same problem as I made this tutorial yesterday and in my case in .env the HOST property was missing.

It needs to be set to something like:

 

HOST='https://6431e05c.ngrok.io.{devshopname}.myshopify.com'

Of course changed to your actual ngrok forward url.

Maybe this is also your case.

fky
New Member
1 0 1

How to set this? Thank you!

JohnEmad
New Member
1 0 1

I have tried this solution, and added the missing HOST property in the .env file,

but unfortunately the error still exist

Here is the error message:

Annotation 2020-07-16 183133.png

 

and here is the snippet from the server.js:

Annotation 2020-07-16 182953.png

whitehorse0324
New Member
2 0 0

You set fixed url into .env file.

By the way, when I try to make public app..?

 

0 Likes

Hi there

I cannot see any relation between the HOST property and the shopOrigin cookie.

The reason why you get this error is because in _app.js the config object for the app-bridge-react Provider has undefined value for shopOrigin. In the demo app the value is set from the cookie named shopOrigin. Which, on the other hand, is set on afterAuth by koa-auth-shopify. Check those to track if shopOrigin is set properly. 

https://apps.shopify.com/picamaze
Animated watermarks for product images and ads
0 Likes
jackcylin
New Member
2 0 1

The problem is about shopOrigin cookie set in afterAuth. Re-login your shop account will help and that's how I solved the same issue. Jack

whitehorse0324
New Member
2 0 0

This code works well in almost browsers.

But, in safari, cookie doesn't work and it goes circled cycle.

Could you give us the code which works well in safari browser, too?

 

Thanks

0 Likes
aqoraan
New Member
6 0 0

@jackcylin wrote:

The problem is about shopOrigin cookie set in afterAuth. Re-login your shop account will help and that's how I solved the same issue. Jack


This solution has fixed it from me, thank you @jackcylin!

0 Likes
jackcylin
New Member
2 0 1

Glad it helps. However, this way does not cover all, since afterAuth wasn't invoked all the time (follow Shopify tutorial). My final solution is to attach a middleware to check if shopOrigin exists and update one if needed. I am not sure if this way is the right answer, but it works for me.

 

    server.use(async (ctx, next) => {
            const got = ctx.cookies.get("shopOrigin");
            if (!got && ctx.request.query) {
                const { hmac, shop } = ctx.request.query;
                if (hmac && shop) {
                    const valid = validateHMAC(
                        hmac,
                        SHOPIFY_API_SECRET_KEY,
                        ctx.request.query
                    );
                    if (valid) {
                        ctx.cookies.set("shopOrigin", shop, {
                            httpOnly: false,
                            secure: true,
                            sign: true,
                            sameSite: "none"
                        });
                        ctx.redirect(
                            `https://${shop}/admin/apps/${SHOPIFY_API_KEY}`
                        );
                    }
                }
            }
            await next();
    });

 

0 Likes