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

Code before:
        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(
        ({ challengeType }) => challengeType === 12 || challengeType === 13
      );

      const challenge = codeRoadChallenges.find(challenge => {
        return challenge.url?.endsWith(tutorialRepo ?? '');
      });

      if (!challenge) {
        void reply.code(400);
        return { type: 'error', msg: 'Tutorial name is not valid' } as const;
      }

      const { id: challengeId, challengeType } = challenge;
      try {
        const tokenInfo = await fastify.prisma.userToken.findUnique({
          where: { id: userToken }
        });

        if (!tokenInfo)
          return { type: 'error', msg: 'User token not found' } as const;

        const { userId } = tokenInfo;

        const user = await fastify.prisma.user.findFirstOrThrow({
          where: { id: userId }
        });

        if (!user)
          return {
            type: 'error',

Review comment (bug):
```suggestion
        return challenge.url?.endsWith(tutorialRepo as string);
```

It looks like the `?? ''` is cause of typescript. It seems to work and I don't see it causing any bugs. Would it be better to do something like this suggestion?

Code after:
          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(
        ({ challengeType }) => challengeType === 12 || challengeType === 13
      );

      const challenge = codeRoadChallenges.find(challenge => {
        return tutorialRepo && challenge.url?.endsWith(tutorialRepo);
      });

      if (!challenge) {
        void reply.code(400);
        return { type: 'error', msg: 'Tutorial name is not valid' } as const;
      }

      const { id: challengeId, challengeType } = challenge;
      try {
        const tokenInfo = await fastify.prisma.userToken.findUnique({
          where: { id: userToken }
        });

        if (!tokenInfo) {
          void reply.code(400);
          return { type: 'error', msg: 'User token not found' } as const;
        }

        const { userId } = tokenInfo;