Questions and discussions about using the Shopify CLI and Shopify-built libraries.
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"
this is the server js
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
Solved! Go to the solution
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.
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.
How to set this? Thank you!
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:
and here is the snippet from the server.js:
You set fixed url into .env file.
By the way, when I try to make public app..?
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.
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
@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!
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();
});
I tried your last solution jackcylin, but I have a new error message :
ReferenceError: validateHMAC is not defined
Any idea to fix this ReactBridge issue ?
Thanks ! 🙂
Arthur
Frankly, re-login is not the solution to this issue. Browser doesn't find shopOrigin cookie causing the issue. There are many reasons cookie not found. You need to see Shopify development team upgraded app bridge from 3x to 4.2.x to remove 3'rd party cookie dependency via JWT session token.
To fix the problem you ran into, my former solution was to check cookie and add again if cookie not found, please see code segment in the below.
const { verifyRequest, validateHMAC } = require("@shopify/koa-shopify-auth");
server.use(async (ctx, next) => {
try {
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();
} catch (err) {
log.error(err.stack);
}
});
how to get shop url in backend or serverside? I want shop url in index.js and middleware.
I want to call this api
https://{shop}/admin/api/2021-04/script_tags.json
how to get value of shop?
thanks @jackcylin for your response. I get this error ReferenceError: window is not defined with window.Shopify
thats working in browser console but I want it in shopify node app in ndex.js or middleware, it doesnt work there and i get error.
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