Open samuel845 opened 2 years ago
As the error says, in your code there doesn't seem to be a call to Shopify.Context.Initialize()
. You should take a look at https://github.com/Shopify/shopify-api-node/blob/main/docs/getting_started.md#set-up-context for details. If you didn't have this before in your code, you may have been using an older version of the Shopify API library.
@TheSecurityDev Thank a lot, I test this and let you know
I added as suggested but get the following error on npm start now.
Cannot read properties of undefined (reading 'initialize')
at Object.
const session = require('koa-session');
dotenv.config();
const port = parseInt(process.env.PORT, 10) || 3000; const dev = process.env.NODE_ENV !== 'production'; const app = next({dev}); const handle = app.getRequestHandler();
const {SHOPIFY_API_SECRET_KEY, SHOPIFY_API_KEY} = process.env; const {createShopifyAuth} = require("simple-koa-shopify-auth"); const {verifyRequest} = require("simple-koa-shopify-auth"); const verifyApiRequest = verifyRequest({returnHeader: true}); const verifyPageRequest = verifyRequest(); const Shopify =require ('@shopify/shopify-api'); const { ApiVersion }=require ('@shopify/shopify-api'); const {API_KEY, API_SECRET_KEY, SCOPES, SHOP, HOST, HOST_SCHEME} = process.env; Shopify.Context.initialize({ API_KEY, API_SECRET_KEY, SCOPES: [SCOPES], HOST_NAME: HOST.replace(/https?:\/\//, ""), HOST_SCHEME, IS_EMBEDDED_APP: true, API_VERSION: ApiVersion.version // all supported versions are available, as well as "unstable" and "unversioned" }); 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_orders,write_orders,read_shipping,read_products,write_products'],
accessMode: 'offline',
authPath: "/auth", // Where to redirect if the session is invalid
async afterAuth(ctx) {
const {shop, accessToken} = ctx.state.shopify;
// ctx.cookies.set('shopOrigin', shop, {httpOnly: false});
// ctx.cookies.set('shopOrigin', shop, {httpOnly: false,secure: true,sameSite: 'none'});
const axios = require('axios');
const https = require('https');
// return this promise
const agent = new https.Agent({
rejectUnauthorized: false
});
await axios.get('https://qikink.com/erp2/index.php/login/shopify?json=1&shop=' + shop + "&accesstoken=" + accessToken, {httpsAgent: agent}).then((response) => {
// console.log('got response' + res);
if (response['data'].error) {
console.log("Below is the error");
console.log(response['data'].error);
return ctx.redirect("/");
// ctx.redirect("/");
// return ctx.redirect('https://qikink.com/erp2/index.php/login/shopify?json=0&shop=' + shop + "&accesstoken=" + accessToken);
} else {
console.log("No Error");
console.log(response);
return ctx.redirect("/");
}
});
console.log("why coming here");
},
}),
);
server.use(verifyRequest());
server.use(async (ctx) => {
await handle(ctx.req, ctx.res);
ctx.respond = false;
ctx.res.statusCode = 200;
return;
});
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`);
});
}); `
Below is my env file.
SHOPIFY_API_KEY=xxx SHOPIFY_API_SECRET_KEY=xx API_KEY=xx API_SECRET_KEY=xx SCOPES=read_orders,write_orders,read_shipping,read_products,write_products SHOP=shopifyapp.qikink.com HOST_SCHEME=https HOST=localhost
This is my package.json , should i include shopify-api here. { "name": "qikink-dropshipping", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "node server.js", "build": "next build", "start": "NODE_ENV=production node server.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "@babel/plugin-transform-react-jsx": "^7.19.0", "@shopify/app-bridge": "^1.26.2", "@shopify/app-bridge-react": "^1.26.2", "@shopify/polaris": "^4.27.0", "@zeit/next-css": "^1.0.1", "asset-require-hook": "^1.2.0", "axios": "^0.19.2", "axios-curlirize": "^1.3.5", "babel-preset-env": "^1.7.0", "dotenv": "^8.2.0", "esm": "^3.2.25", "express": "^4.17.1", "form-data": "^3.0.0", "isomorphic-fetch": "^2.2.1", "js-cookie": "^2.2.1", "koa": "^2.13.0", "koa-session": "^5.13.1", "next": "^9.5.2", "nohup": "^0.1.0", "react": "^16.13.1", "react-dom": "^16.13.1", "react-string-replace": "^0.4.4", "request": "^2.88.2", "request-ip": "^2.1.3", "simple-koa-shopify-auth": "^2.1.5", "sqlite3": "^5.1.2", "typescript": "^3.9.7" }, "devDependencies": { "@babel/plugin-proposal-class-properties": "^7.18.6" } }
Hi I was using koa auth for shopify, now since its depreciated, tried this library, first i was running koa auth on node 10.18.0 , on that , installing it gave me a error on redis, so upgraded node to 16.x and npm run build and npm start works fine, but when accessing the app, it crashes with Error: Context has not been properly initialized. Please call the .initialize() method to setup your app context object. I am very new to node js , pls help, below is my code in server.js
/** require('isomorphic-fetch'); const dotenv = require('dotenv'); const Koa = require('koa'); const next = require('next'); //const {default: createShopifyAuth} = require('@shopify/koa-shopify-auth'); //const {verifyRequest} = require('@shopify/koa-shopify-auth'); const session = require('koa-session');
dotenv.config();
const port = parseInt(process.env.PORT, 10) || 3000; const dev = process.env.NODE_ENV !== 'production'; const app = next({dev}); const handle = app.getRequestHandler();
const {SHOPIFY_API_SECRET_KEY, SHOPIFY_API_KEY} = process.env; const { createShopifyAuth } = require("simple-koa-shopify-auth"); const { verifyRequest } = require("simple-koa-shopify-auth"); const verifyApiRequest = verifyRequest({ returnHeader: true }); const verifyPageRequest = verifyRequest(); app.prepare().then(() => { const server = new Koa();
});
*//