-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (109 loc) · 3.38 KB
/
index.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
116
117
118
119
120
121
122
123
124
125
import express from 'express';
import bodyParser from 'body-parser';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import { makeExecutableSchema } from 'graphql-tools';
import path from 'path';
import cors from 'cors';
import { fileLoader, mergeTypes, mergeResolvers } from 'merge-graphql-schemas';
import jwt from 'jsonwebtoken';
import { createServer } from 'http';
import { execute, subscribe } from 'graphql';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import models from './models';
import { refreshTokens } from './auth';
const typesArray = fileLoader(path.join(__dirname, './graphql/schema'));
const resolversArray = fileLoader(path.join(__dirname, './graphql/resolvers'));
const typeDefs = mergeTypes(typesArray);
const resolvers = mergeResolvers(resolversArray);
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
const PORT = 8080;
const SECRET = 'somethingsecret';
const SECRET2 = 'somethingsecretmore';
const gqlEndpoint = '/graphql';
const app = express();
app.use(cors('http://localhost:3000'));
const addUserMiddleware = async (req, res, next) => {
const token = req.headers['x-token'];
if (token) {
try {
const { user } = jwt.verify(token, SECRET);
req.user = user;
} catch (err) {
const refreshToken = req.headers['x-refresh-token'];
const newTokens = await refreshTokens(token, refreshToken, models, SECRET, SECRET2);
if (newTokens.token && newTokens.refreshToken) {
res.set('Access-Control-Expose-Headers', 'x-token', 'x-refresh-token');
res.set('x-token', newTokens.token);
res.set('x-refresh-token', newTokens.refreshToken);
}
req.user = newTokens.user;
}
}
next();
};
app.use(addUserMiddleware);
app.use(
gqlEndpoint,
bodyParser.json(),
graphqlExpress(req => ({
schema,
context: {
models,
user: req.user,
SECRET,
SECRET2,
},
})),
);
app.use(
'/graphiql',
graphiqlExpress({
endpointURL: gqlEndpoint,
subscriptionsEndpoint: 'ws://localhost:8080/subscriptions',
}),
);
const server = createServer(app);
// sync({ force: true }) //force for dropping
models.sequelize.sync().then(() => {
console.log('Db is synced');
server.listen(PORT, () => {
// eslint-disable-next-line no-new
new SubscriptionServer(
{
execute,
subscribe,
schema,
// onConnect: async ({ token, refreshToken }, webSocket) => {
// if (token && refreshToken) {
// let user = null;
// try {
// const payload = jwt.verify(token, SECRET);
// user = payload.user;
// } catch (err) {
// const newTokens = await refreshTokens(token, refreshToken, models, SECRET, SECRET2);
// user = newTokens.user;
// }
// if (!user) {
// throw new Error('Invalid auth tokens');
// }
// const member = await models.Member.findOne({ where: { teamId: 1, userId: user.id } });
// if (!member) {
// throw new Error('Missing auth tokens!');
// }
// return true;
// }
// // throw new Error('Missing auth tokens!');
// return true;
// },
},
{
server,
path: '/subscriptions',
},
);
console.log(`Listening on port ${PORT}...`);
});
});