Repository: freeCodeCamp/freeCodeCamp
File: api/src/routes/challenge.ts (TypeScript)

Code before:

  fastify.post(
    '/coderoad-challenge-completed',
    {
      schema: schemas.coderoadChallengeCompleted,
      attachValidation: true
    },
    async (req, reply) => {
      let userToken;

      const { 'coderoad-user-token': encodedUserToken } = req.headers;
      const { tutorialId } = req.body;

      if (!tutorialId) {
        void reply.code(400);
        return {
          type: 'error',
          msg: `'tutorialId' not found in request body`
        } as const;
      }

      if (!encodedUserToken) {
        void reply.code(400);
        return {
          type: 'error',
          msg: `'coderoad-user-token' not found in request headers`
        } as const;
      }

      try {
        if (typeof encodedUserToken === 'string' && jwtSecret) {
          const payload = jwt.verify(encodedUserToken, jwtSecret) as JwtPayload;
          userToken = payload.userToken;
        }
      } catch {
        void reply.code(400);
        return { type: 'error', msg: `invalid user token` } as const;
      }

      const tutorialRepo = tutorialId?.split(':')[0];
      const tutorialOrg = tutorialRepo?.split('/')?.[0];

      if (tutorialOrg !== 'freeCodeCamp') {
        void reply.code(400);
        return {
          type: 'error',
          msg: `Tutorial not hosted on freeCodeCamp GitHub account`
        } as const;
      }

      const codeRoadChallenges = challenges.filter(

Review comment (refactor):
```suggestion
          msg: `'Coderoad-User-Token' not found in request headers`
```

While on the topic, can you create an issue to refactor the header to include a meaningful prefix like `FCC-` to avoid any clashes, as the header right now is too generic? Thanks.

Code after:

  fastify.post(
    '/coderoad-challenge-completed',
    {
      schema: schemas.coderoadChallengeCompleted,
      attachValidation: true
    },
    async (req, reply) => {
      let userToken;

      const { 'coderoad-user-token': encodedUserToken } = req.headers;
      const { tutorialId } = req.body;

      if (!tutorialId) {
        void reply.code(400);
        return {
          type: 'error',
          msg: `'tutorialId' not found in request body`
        } as const;
      }

      if (!encodedUserToken) {
        void reply.code(400);
        return {
          type: 'error',
          msg: `'Coderoad-User-Token' not found in request headers`
        } as const;
      }

      try {
        if (typeof encodedUserToken === 'string' && jwtSecret) {
          const payload = jwt.verify(encodedUserToken, jwtSecret) as JwtPayload;
          userToken = payload.userToken;
        }
      } catch {
        void reply.code(400);
        return { type: 'error', msg: `invalid user token` } as const;
      }

      const tutorialRepo = tutorialId?.split(':')[0];
      const tutorialOrg = tutorialRepo?.split('/')?.[0];

      if (tutorialOrg !== 'freeCodeCamp') {
        void reply.code(400);
        return {
          type: 'error',
          msg: `Tutorial not hosted on freeCodeCamp GitHub account`
        } as const;
      }

      const codeRoadChallenges = challenges.filter(