Code Monkey home page Code Monkey logo

Comments (4)

TheSecurityDev avatar TheSecurityDev commented on July 29, 2024

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.

from simple-koa-shopify-auth.

samuel845 avatar samuel845 commented on July 29, 2024

@TheSecurityDev Thank a lot, I test this and let you know

from simple-koa-shopify-auth.

samuel845 avatar samuel845 commented on July 29, 2024

I added as suggested but get the following error on npm start now.
Cannot read properties of undefined (reading 'initialize')
at Object. (C:\xampp_7\htdocs\shopify_Server_upload\shopifyappnew\server.js:24:17)
If I use import, its giving me ES6 syntax error "cannot use import outside modules". so I changed the syntax as below.
`require('isomorphic-fetch');
const dotenv = require('dotenv');
const Koa = require('koa');
const next = require('next');

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

from simple-koa-shopify-auth.

samuel845 avatar samuel845 commented on July 29, 2024

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"
}
}

from simple-koa-shopify-auth.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.