Hacker News Node Tutorial

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.

@Christian510 can you share a link to the tutorial you’re trying?

Here you go. Thanks! Node Tutorial

@Christian510 what is the value of authHeader in your getUserId function?

function getUserId(req, authToken) {
  if (req) {
    const authHeader = req.headers.authorization;

    // What is value of `authHeader`?
    console.log(authHeader);

    // ... everything else
}

@hwillson Thanks for you help. I get the following:

authHeader:  Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjcsImlhdCI6MTYyODAwMzMyNX0.-CVMy6ZnIWgGz5JW8cBi8DPuJCFBVxh0hLRct-mPHDg

Oops! I made a mistake in this post that may have caused some confusion. getTokenPayload(token) should be getTokenPayload(authToken). That’s how the tutorial is set up. I mistakenly, typed in a different parameter I was testing. Hope that helps clear things up.

I believe I solved this issue. I believe the id is suppose to be a number and not a string of numbers and characters as the tutorial shows. Anyway it’s all working as expected now that I understand clearly what is being referenced and why. @hwillson thanks for attempting to help me with my confusion! :grinning_face_with_smiling_eyes:

1 Like