Created
May 9, 2020 09:09
-
-
Save vvastdev/48abf3db87110aeb18383b9797e70c29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
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_products'], | |
afterAuth(ctx) { | |
const { shop, accessToken } = ctx.session; | |
ctx.redirect('/'); | |
}, | |
}), | |
); | |
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}`); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment