Last active
May 24, 2019 15:14
-
-
Save OlivierJM/a0270d41c1df0081600c4f70217abf67 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 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