Skip to content

Instantly share code, notes, and snippets.

@OlivierJM
Last active May 24, 2019 15:14
Show Gist options
  • Save OlivierJM/a0270d41c1df0081600c4f70217abf67 to your computer and use it in GitHub Desktop.
Save OlivierJM/a0270d41c1df0081600c4f70217abf67 to your computer and use it in GitHub Desktop.
const SECRET="createaverystrongsecretthatalsoincludes2423412wdsa324e34e"
const server = new ApolloServer({
schema,
context: ({ req }) => {
const { user } = req
// the user and secret we are passing here is what we access in every resolver
return {
user,
SECRET,
}
},
})
// this is a simple middleware that will check when every request hits apollo server.
const authUser = async req => {
const token = await req.headers['authentication']
try {
const { user } = await jwt.verify(token, process.env.SECRET)
req.user = user
} catch (error) {
console.log(error) // or do something else with the error
}
req.next()
}
// now we can pass our middleware
appServer.use(authUser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment