Shopify / koa-shopify-auth

DEPRECATED Middleware to authenticate a Koa application with Shopify
MIT License
80 stars 63 forks source link

AppBridgeError Koa version 5 #171

Closed coreway1 closed 1 year ago

coreway1 commented 2 years ago

I have used the "@shopify/koa-shopify-auth": "^5.0.3" and "@shopify/shopify-api": "^5.0.1"

after upgrade version of koa getting this error :

Uncaught s {name: 'AppBridgeError', message: 'APP::ERROR::INVALID_CONFIG: apiKey must be provided', action: undefined, type: 'APP::ERROR::INVALID_CONFIG', stack: 'AppBridgeError: APP::ERROR::INVALID_CONFIG: apiKey must be provided'}

Here is my server code

require('isomorphic-fetch');

const dotenv = require('dotenv');
const next = require('next');

// Koa-related
const http = require('http');
const Koa = require('koa');
const cors = require('@koa/cors');
const socket = require('socket.io');
const bodyParser = require('koa-bodyparser');
//const vhost = require('koa-virtual-host');

const {
  default: shopifyAuth,
  verifyRequest,
} = require('@shopify/koa-shopify-auth');

const { default: Shopify, ApiVersion } = require('@shopify/shopify-api');

const session = require('koa-session');
const { default: graphQLProxy } = require('@shopify/koa-shopify-graphql-proxy');
const Router = require('koa-router');

// initializes the library
Shopify.Context.initialize({
  API_KEY: process.env.PROD_SHOPIFY_API_KEY,
  API_SECRET_KEY: process.env.PROD_SHOPIFY_API_SECRET_KEY,
  SCOPES: [
    'read_orders',
    'write_orders',
    'read_products',
    'write_products',
    'read_customers',
    'write_customers',
    'write_draft_orders',
    'read_draft_orders',
    'read_script_tags',
    'write_script_tags',
  ],
  HOST_NAME: process.env.PROD_SERVER_URL,
  API_VERSION: ApiVersion.April22,
  IS_EMBEDDED_APP: true,
  SESSION_STORAGE: new Shopify.Session.MemorySessionStorage(),
});

   server.use(
    shopifyAuth({
      accessMode: 'offline',
        apiKey: SHOPIFY_API_KEY,
        secret: SHOPIFY_API_SECRET_KEY,
        accessMode: 'offline',
        scopes: [
          'read_orders',
          'write_orders',
          'read_products',
          'write_products',
          'read_customers',
          'write_customers',
          'write_draft_orders',
          'read_draft_orders',
          'read_script_tags',
          'write_script_tags',
        ],
        async afterAuth(ctx) {

 const {shop, accessToken} = ctx.state.shopify;

       ACTIVE_SHOPIFY_SHOPS[shop] = true;

 // Redirect user to app home page

        ctx.redirect(`/?shop=${shop}`);
      },
    })
  );

  router.get('*', verifyRequest({accessMode: 'offline'}), async (ctx) => {
    //router.get('*', async (ctx) => {
    if (ctx.url.includes('/?')) {
      if (ctx.url.substring(0, 2) != '/?') {
        ctx.url = ctx.url.replace('/?', '?'); // Remove trailing slash before params
        ctx.url = ctx.url.replace(/\/\s*$/, ''); // Remove trailing slash at the end
      }
    }

    await handle(ctx.req, ctx.res);
    ctx.respond = false;
    ctx.res.statusCode = 200;
  });

Below you can find the screenshot of error I'm getting. error-snap

Can you help me if I'm doing anything wrong here?

github-actions[bot] commented 1 year ago

Note that this repo is no longer maintained and this issue will not be reviewed. Prefer the official JavaScript API library. If you still want to use Koa, see simple-koa-shopify-auth for a potential community solution.