-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.js
More file actions
26 lines (23 loc) · 734 Bytes
/
Copy pathapp.js
File metadata and controls
26 lines (23 loc) · 734 Bytes
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
const { ApolloServer } = require('apollo-server');
const jwt = require('jsonwebtoken');
const typeDefs = require('./graphQL/schemas');
const resolvers = require('./graphQL/resolvers');
function getUser(rawToken) { // eslint-disable-line consistent-return
if (rawToken.length) {
// Bearer eyJ0eXAiOiJKV1Q...
const token = rawToken.slice(7, rawToken.length);
// return the decoded user if the token is valid
return jwt.verify(token, process.env.JWT_SECRET);
}
}
const app = new ApolloServer({
typeDefs,
resolvers,
// Create token for validation
context: ({ req }) => {
const token = req.headers.authorization || '';
const user = getUser(token);
return { user };
},
});
module.exports = app;