Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import createShopifyAuth, {
createVerifyRequest,
} from '@shopify/koa-shopify-auth';
import renderReactApp from './render-react-app';
import webpack from 'koa-webpack';
import graphQLProxy from '@shopify/koa-shopify-graphql-proxy';
dotenv.config();
const {SHOPIFY_API_KEY, SHOPIFY_SECRET} = process.env;
const app = new Koa();
app.use(session(app));
app.use(
createShopifyAuth({
// your shopify app's api key
apiKey: SHOPIFY_API_KEY,
// your shopify app's api secret
secret: SHOPIFY_SECRET,
// our app's permissions
// we need to write products to the user's store
scopes: ['write_products'],
// our own custom logic after authentication has completed
afterAuth(ctx) {
const {shop, accessToken} = ctx.session;
console.log('We did it!', shop, accessToken);
ctx.redirect('/');
},
}),
afterAuth(ctx) {
const {shop, accessToken} = ctx.session;
console.log('We did it!', shop, accessToken);
ctx.redirect('/');
},
}),
);
app.keys = [SHOPIFY_SECRET];
// secure all middleware after this line
app.use(createVerifyRequest());
app.use(async function(ctx, next) {
// log 'middleware 1'
console.log('middleware 1');
// then pause and wait for the next middleware
await next();
// then log 'back to middleware 1'
console.log('back to middleware 1');
});
app.use(webpack());
app.use(graphQLProxy);
app.use(renderReactApp);
.then(() => {
const server = new Koa()
const router = new Router()
server.keys = [ process.env.SERVER_SECRET ]
server
.use(session(server))
.use(shopifyAuth({
// if specified, mounts the routes off of the given path
// eg. /shopify/auth, /shopify/auth/callback
// defaults to ''
prefix: '/shopify',
// your shopify app api key
apiKey: process.env.SHOPIFY_API_KEY,
// your shopify app secret
secret: process.env.SHOPIFY_SECRET,
// scopes to request on the merchants store
scopes: ['write_orders, write_products'],
// callback for when auth is completed
afterAuth(ctx) {
// add/install shop
const
{shop, accessToken} = ctx.session,
client = initApollo()
console.log('Failed to register webhook', registration.result);
}
await getSubscriptionUrl(ctx, accessToken, shop);
}
})
);
const webhook = receiveWebhook({ secret: SHOPIFY_API_SECRET_KEY });
router.post('/webhooks/products/create', webhook, (ctx) => {
console.log('received webhook: ', ctx.state.webhook);
});
server.use(graphQLProxy({ version: ApiVersion.April19 }));
router.get('*', verifyRequest(), async (ctx) => {
await handle(ctx.req, ctx.res);
ctx.respond = false;
ctx.res.statusCode = 200;
});
server.use(router.allowedMethods());
server.use(router.routes());
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`);
});
});
app.prepare().then(() => {
const server = new Koa();
const router = new Router();
server.use(session(server));
server.keys = [SHOPIFY_API_SECRET_KEY];
server.use(
createShopifyAuth({
apiKey: SHOPIFY_API_KEY,
secret: SHOPIFY_API_SECRET_KEY,
scopes: ['read_products', 'write_products'],
async afterAuth(ctx) {
const { shop, accessToken } = ctx.session;
ctx.cookies.set("shopOrigin", shop, { httpOnly: false });
const registration = await registerWebhook({
address: `${HOST}/webhooks/products/create`,
topic: 'PRODUCTS_CREATE',
accessToken,
shop,
apiVersion: ApiVersion.October19
});
if (registration.success) {