forked from kinngh/shopify-nextjs-prisma-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
115 lines (98 loc) · 3.15 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
112
113
114
115
require("dotenv").config();
const Koa = require("koa");
const {
default: createShopifyAuth,
verifyRequest,
} = require("@shopify/koa-shopify-auth");
const { default: Shopify } = require("@shopify/shopify-api");
const next = require("next");
const Router = require("koa-router");
const mongoose = require("mongoose");
const sessionStorage = require("./utils/sessionStorage.js");
const SessionModel = require("./models/SessionModel.js");
const webhooksRegistrar = require("./webhooks/_webhooksRegistrar.js");
const webhookRouters = require("./webhooks/_webhookRouters.js");
const userRoutes = require("./routes/index.js");
const port = parseInt(process.env.PORT, 10) || 8081;
const dev = process.env.NODE_ENV !== "production";
const app = next({
dev,
});
const handle = app.getRequestHandler();
//MARK:- MongoDB Connection
const mongoUrl =
process.env.MONGO_URL || "mongodb://127.0.0.1:27017/shopify-app";
mongoose.connect(
mongoUrl,
{
useNewUrlParser: true,
useUnifiedTopology: true,
},
(err) => {
if (err) {
console.log("--> There was an error connecting to MongoDB:", err.message);
} else {
console.log("--> Connected to MongoDB");
}
}
);
//MARK:- Shopify Init
Shopify.Context.initialize({
API_KEY: process.env.SHOPIFY_API_KEY,
API_SECRET_KEY: process.env.SHOPIFY_API_SECRET,
SCOPES: process.env.SHOPIFY_API_SCOPES.split(","),
HOST_NAME: process.env.SHOPIFY_APP_URL.replace(/https:\/\//, ""),
API_VERSION: process.env.SHOPIFY_API_VERSION,
IS_EMBEDDED_APP: true,
SESSION_STORAGE: sessionStorage,
});
app.prepare().then(async () => {
const server = new Koa();
const router = new Router();
server.keys = [Shopify.Context.API_SECRET_KEY];
server.use(
createShopifyAuth({
async afterAuth(ctx) {
const { shop, accessToken, scope } = ctx.state.shopify;
const host = ctx.query.host;
webhooksRegistrar(shop, accessToken); //MARK:- TODO | Check to see if the store already has webhooks setup to avoid the failing re-registration of webhooks
ctx.redirect(`/?shop=${shop}&host=${host}`);
},
})
);
const handleRequest = async (ctx) => {
await handle(ctx.req, ctx.res);
ctx.respond = false;
ctx.res.statusCode = 200;
};
router.post(
"/graphql",
verifyRequest({ returnHeader: true }),
async (ctx, next) => {
await Shopify.Utils.graphqlProxy(ctx.req, ctx.res);
}
);
router.get("(/_next/static/.*)", handleRequest); // Static content is clear
router.get("/_next/webpack-hmr", handleRequest); // Webpack content is clear
router.get("(.*)", async (ctx) => {
const shop = ctx.query.shop;
const findShopCount = await SessionModel.countDocuments({
shop,
isActive: true,
});
if (findShopCount < 1) {
await SessionModel.findOneAndUpdate({ shop }, { isActive: true });
ctx.redirect(`/auth?shop=${shop}`);
} else {
await handleRequest(ctx);
}
});
server.use(router.allowedMethods());
server.use(router.routes());
//MARK:- Routes and routers
server.use(webhookRouters());
server.use(userRoutes());
server.listen(port, () => {
console.log(`--> Ready on http://localhost:${port}`);
});
});