-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
111 lines (83 loc) · 2.71 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import 'regenerator-runtime/runtime';
import shopifyAuth from '@shopify/koa-shopify-auth';
import Shopify, { ApiVersion } from '@shopify/shopify-api';
import mongoose from 'mongoose';
import Cryptool from './cryptool';
import { TokenService } from './services';
import authRequest from './middleware';
import logger from './utils';
import Koa from 'koa';
import KoaRouter from '@koa/router';
import nextJs from 'next';
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = nextJs({ dev });
const handle = app.getRequestHandler();
mongoose.connect(process.env.MONGODB_CONNECTION_STRING);
Shopify.Context.initialize({
API_KEY: process.env.SHOPIFY_API_KEY,
API_SECRET_KEY: process.env.SHOPIFY_API_SECRET,
SCOPES: ['read_customers'],
HOST_NAME: process.env.HOST,
API_VERSION: ApiVersion.October20,
IS_EMBEDDED_APP: false,
SESSION_STORAGE: new Shopify.Session.MemorySessionStorage(),
});
const handleRequest = async (ctx) => {
await handle(ctx.req, ctx.res);
ctx.respond = false;
ctx.res.statusCode = 200;
};
app.prepare().then(() => {
const server = new Koa();
const router = new KoaRouter();
server.keys = [process.env.SHOPIFY_API_KEY];
server.use(
shopifyAuth({
accessMode: 'offline',
afterAuth: async (ctx) => {
const { shop, accessToken } = ctx.state.shopify;
logger.info(`${shop} authenticated`);
await TokenService.upsertToken(shop, accessToken);
ctx.cookies.set('shop', Cryptool.encrypt(JSON.stringify({ shop })), {
signed: true,
overwrite: true,
secure: true,
});
logger.info(`${shop} cookies set`);
await Shopify.Webhooks.Registry.register({
shop,
accessToken,
path: '/webhooks',
topic: 'APP_UNINSTALLED',
webhookHandler: async (_topic, shopName) => {
logger.info(`Handling ${shop} uninstall`);
await TokenService.removeToken(shopName);
},
});
logger.info(`${shop} uninstall hook registered`);
ctx.redirect('/');
return;
},
}),
);
router.post('/webhooks', async (ctx) => {
try {
await Shopify.Webhooks.Registry.process(ctx.req, ctx.res);
} catch (error) {
logger.error(error);
}
});
router.get('/install', handleRequest);
router.get('(/_next/static/.*)', handleRequest);
router.get('/_next/webpack-hmr', handleRequest);
router.all('(.*)', authRequest, handleRequest);
server.use(async (ctx, next) => {
ctx.res.statusCode = 200;
await next();
});
server.use(router.routes());
server.listen(port, () => {
logger.info(`Server ready on http://localhost:${port}`);
});
});