-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
50 lines (41 loc) · 1.11 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
import express from 'express';
import cors from 'cors';
import { ApolloServer } from 'apollo-server-express';
import dotenv from 'dotenv';
import conectarBD from './db/db.js';
import { tipos } from './graphql/types.js';
import { resolvers } from './graphql/resolvers.js';
import { validateToken } from './token/tokenUtils.js';
dotenv.config();
const getUserData = (token) => {
const verificacion = validateToken(token.split(' ')[1]);
if (verificacion.data) {
return verificacion.data;
} else {
return null;
}
};
const server = new ApolloServer({
typeDefs: tipos,
resolvers: resolvers,
context: ({ req, res }) => {
const token = req.headers?.authorization ?? null;
if (token) {
const userData = getUserData(token);
console.log('user data', userData);
if (userData) {
return { userData };
}
}
return null;
},
});
const app = express();
app.use(express.json());
app.use(cors());
app.listen({ port: process.env.PORT || 4000 }, async () => {
await conectarBD();
await server.start();
server.applyMiddleware({ app });
console.log('Servidor listo');
});