-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
50 lines (48 loc) · 1.54 KB
/
app.js
File metadata and controls
50 lines (48 loc) · 1.54 KB
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
const { GraphQLError } = require("graphql");
const { startStandaloneServer } = require("@apollo/server/standalone");
const { server } = require("./index.js");
const jwt = require("jsonwebtoken");
(async () => {
const { url } = await startStandaloneServer(server, {
listen: { port: process.env.PORT || 4000 },context: async ({ req, res }) => {
return {
authentication: async () => {
if (!req.headers.authorization) {
throw (
(new GraphQLError("Access token must be provided"),
{
extensions: {
code: "UNAUTHORIZED",
},
})
);
}
const access_token = req.headers.authorization.split(" ")[1];
if (!access_token) {
throw (
(new GraphQLError("Access token must be provided"),
{
extensions: {
code: "UNAUTHORIZED",
},
})
);
}
const decoded_token = jwt.verify(access_token, process.env.JWT_SECRET);
if (!decoded_token) {
throw (
(new GraphQLError("Access token must be valid"),
{
extensions: {
code: "UNAUTHORIZED",
},
})
);
}
return decoded_token;
},
};
}
});
console.log(`🚀 Server ready at: ${url}`);
})();