Created
September 10, 2019 10:25
-
-
Save tkssharma/ac3c4faaac5d9f3db639c9d16d8af4f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getLoggedInUser = req => { | |
const token = req.headers['x-auth-token']; | |
if (token) { | |
try { | |
return jwt.verify(token, process.env.JWT_SECRET); | |
} catch(error) { | |
throw new Error('Session expired'); | |
} | |
} | |
}; | |
const server = new ApolloServer({ | |
typeDefs, | |
resolvers, | |
context: ({ req }) => ({ | |
models, | |
secret: process.env.JWT_SECRET, | |
me: getLoggedInUser(req) | |
}) | |
}); | |
server.applyMiddleware({ app }); | |
app.listen(3000, () => console.info('Apollo GraphQL server is running on port 3000')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment