-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (32 loc) · 946 Bytes
/
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
import express from "express";
import { ApolloServer } from "apollo-server-express";
import dotenv from "dotenv";
import jwt from "jsonwebtoken";
import { typeDefs } from "./database/schema";
import { resolvers } from "./database/resolvers";
dotenv.config({ path: "variables.env" });
const app = express();
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({ req }) => {
const token = req.headers["authorization"];
// console.log(token);
if (token !== "null") {
try {
const currentUser = await jwt.verify(token, process.env.SECRET);
//console.log(currentUser);
req.currentUser = currentUser;
return { currentUser };
} catch (error) {
console.log(error);
}
}
}
});
server.applyMiddleware({ app });
app.listen({ port: process.env.PORT }, () =>
console.log(
`The server is running on http://localhost:4000${server.graphqlPath}`
)
);