Created
January 2, 2021 23:38
-
-
Save nutterbrand/12eac56b53cccd5c73a4dbd26a064552 to your computer and use it in GitHub Desktop.
Shopify Node JS Server
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 Koa = require('koa'); | |
const next = require('next'); | |
const { default: createShopifyAuth } = require('@shopify/koa-shopify-auth'); | |
const dotenv = require('dotenv'); | |
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({ sameSite: 'none', secure: true }, 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; | |
}); | |
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