I have run into an issue with this Node tutorial by RobinMacPherson. I am getting errors regarding the authorization. First, I can create a new user. But, the post causes an error I expected to get back some data as the tutorial shows. Instead I get the following error:
{ "errors": [ {
"message": "Context creation failed: jwt must be provided",
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"JsonWebTokenError: Context creation failed: jwt must be provided",
" at Object.module.exports [as verify] (/Users/cthompson/Documents/PromoShopInc/promoshop-training/hackernews-node/node_modules/jsonwebtoken/verify.js:53:17)",
" at getTokenPayload (/Users/cthompson/Documents/PromoShopInc/promoshop-training/hackernews-node/src/utils.js:8:16)",
" at getUserId (/Users/cthompson/Documents/PromoShopInc/promoshop-training/hackernews-node/src/utils.js:22:32)",
" at ApolloServer.context (/Users/cthompson/Documents/PromoShopInc/promoshop-training/hackernews-node/src/index.js:33:23)",
" at ApolloServer.graphQLServerOptions (/Users/cthompson/Documents/PromoShopInc/promoshop-training/hackernews-node/node_modules/apollo-server-core/dist/ApolloServer.js:427:34)",
" at runMicrotasks (<anonymous>)",
" at processTicksAndRejections (internal/process/task_queues.js:95:5)"
]}}} ],
"data": null
}
When I follow the stack trace I see that function getTokenPayload(token)
functions parameter is an undefined value. In function getUserId(req, authToken)
I noticed that the authToken does not have a corresponding argument at index.js:
const server = new ApolloServer({
typeDefs: fs.readFileSync(
path.join(__dirname, 'schema.graphql'),
'utf8' ),
resolvers,
context: ({ req }) => {
return {
...req,
prisma,
userId:
req && req.headers.authorization
? getUserId(req)
: null
};}});
This appears to be causing my error. At utils.js tried replacing authToken
with the variable token
. This gets rid of the errors. But, I do not get an id in return. Just a single numerical value: { "data": {"post": { "id": "7"}}}
. I expected a user id something like, { "data": {"post": { "id": "cjpsi9sdxh9a..."}}}
I have reviewed my code and I am reasonably sure it’s accurate to the tutorial. I can provide the all of my code if needed. Any help would be greatly appreciated.